JavaFX Button和Scene点击事件代码示例

最终效果

JavaFX Button和Scene点击事件代码示例

代码

写在一个类里面

package cn.zxl.StackPane;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * @Description: //TODO
 * @Author: zhangxueliang
 * @Create: 2021-05-27 09:04
 * @Version: 1.0
 **/
public class MyStackPane extends Application implements EventHandler<MouseEvent> {
    Button btn;
    @Override
    public void start(Stage primaryStage) throws Exception {
        btn = new Button("你好");
        btn.setOnMouseClicked(this);
        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(btn);
        Scene scene = new Scene(stackPane, 200, 200);
        scene.setOnMousePressed(this);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void handle(MouseEvent event) {
        if (event.getSource()==btn){
            System.out.println("你点击的是按钮");
        }else {
            System.out.println("你点击了场景");
        }
    }
}

也可以单独提取出事件监听类

按钮事件监听类

package cn.zxl.StackPane;

import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;

/**
 * @Description: //TODO
 * @Author: zhangxueliang
 * @Create: 2021-05-27 09:20
 * @Version: 1.0
 **/
public class MyMouseEvent implements EventHandler<MouseEvent> {
    @Override
    public void handle(MouseEvent event) {
        System.out.println("你点击的是按钮");
    }
}

场景事件监听类

package cn.zxl.StackPane;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;

/**
 * @Description: //TODO
 * @Author: zhangxueliang
 * @Create: 2021-05-27 09:24
 * @Version: 1.0
 **/
public class SceneMouseEvent implements EventHandler<MouseEvent> {
    @Override
    public void handle(MouseEvent event) {
        System.out.println("你点击的是场景");
    }
}

调用

package cn.zxl.StackPane;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * @Description: //TODO
 * @Author: zhangxueliang
 * @Create: 2021-05-27 09:04
 * @Version: 1.0
 **/
public class MyStackPane2 extends Application{
    Button btn;
    @Override
    public void start(Stage primaryStage) throws Exception {
        btn = new Button("你好");
        btn.setOnMouseClicked(new MyMouseEvent());
        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(btn);
        Scene scene = new Scene(stackPane, 200, 200);
        scene.setOnMousePressed(new SceneMouseEvent());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

匿名内部类方式(推荐)
package cn.zxl.StackPane;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * @Description: //TODO
 * @Author: zhangxueliang
 * @Create: 2021-05-27 09:04
 * @Version: 1.0
 **/
public class MyStackPane3 extends Application{
    Button btn;
    @Override
    public void start(Stage primaryStage) throws Exception {
        btn = new Button("你好");
        btn.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("你点击的是按钮");
            }
        });
        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(btn);
        Scene scene = new Scene(stackPane, 200, 200);
        scene.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("你点击的是场景");
            }
        });
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

lambda表达式(新手不推荐)
package cn.zxl.StackPane;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * @Description: //TODO
 * @Author: zhangxueliang
 * @Create: 2021-05-27 09:04
 * @Version: 1.0
 **/
public class MyStackPane4 extends Application{
    Button btn;
    @Override
    public void start(Stage primaryStage) throws Exception {
        btn = new Button("你好");
        btn.setOnMouseClicked(event -> {
            System.out.println("你点击的是按钮");
        });
        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(btn);
        Scene scene = new Scene(stackPane, 200, 200);
        scene.setOnMousePressed(event -> {
            System.out.println("你点击的是场景");
        });
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

 

上一篇:[转自机器之心] 刚入校门的PhD们还可以抢救一下(读研读博指南)


下一篇:js事件简介