您需要挂起一个事件处理程序:
public class CustomButton extends Button implements EventHandler<MouseEvent> {
public CustomButton() {
super("myButton");
}
@Override
public void handle(MouseEvent event) {
if (event.getEventType() == MouseEvent.MOUSE_CLICKED){
setText("hello");
System.out.println("button pressed");
}
}
}
主要的:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Stage stage = primaryStage;
stage.setTitle("Simple Application");
stage.setScene(InitScene());
stage.show();
}
private Scene InitScene(){
Group root = new Group();
Scene myScene = new Scene(root, 300, 300);
root.getChildren().add (new CustomButton());
return myScene;
}
public static void main(String... args){
launch(args);
}
}
问题:当你点击按钮时,绝对没有任何反应。
Java FX技术旨在将fxml文件表示的图形界面和事件处理从图形界面中分离出来,你用awt或swing风格实现一个Java FX应用程序。
要创建事件处理程序:
在 fxml 文件中: