- NORMAL:Creates a simple push button.
- DEFAULT:Creates a simple button that can be operated by enter key.
- CANCEL:Creates a button that can be operated by escape button.
CODING:
import javafx.application.Application;import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class mainfx extends Application
{
@Override
public void start(Stage primaryStage) {
Group g = new Group();
primaryStage.setTitle("FX Button");
Button btn = new Button("Press To Exit");
btn.setMinWidth(100);
btn.setLayoutX(100);
btn.setLayoutY(100);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.exit(1);
}
});
g.getChildren().add(btn);
Scene sc = new Scene(g,400,300);
primaryStage.setScene(sc);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
APPLYING MOUSE EVENT IN BUTTON:
import javafx.application.Application;import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
public class mainfx extends Application {
Button btn = new Button();
@Override
public void start(Stage primaryStage) {
Group g = new Group();
primaryStage.setTitle("Button Event In JavaFx");
btn = new Button("Enter Mouse To Rotate");
btn.setMinWidth(100);
btn.setLayoutX(100);
btn.setLayoutY(100);
btn.addEventHandler(MouseEvent.MOUSE_ENTERED,
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
btn.getTransforms().add(new Rotate(45));
}
});
g.getChildren().add(btn);
Scene sc = new Scene(g,400,300);
primaryStage.setScene(sc);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
0 comments:
Post a Comment