关于JAVAFX鼠标单击案例

在javafx中 鼠标单击事件属于ActionEvent,而不属于mouseAction所以说 如果用的ActionEvent则单击没有效果哟

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Action1 extends Application {
    int count=0;
    Button bt=new Button();
    @Override
    public void start(Stage primaryStage)
    {
        HBox hBox=new HBox();
        hBox.setAlignment(Pos.TOP_CENTER);
        hBox.getChildren().add(bt);
        BorderPane pane =new BorderPane();
        pane.setBottom(hBox);
        bt.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub
                count+=1;
                bt.setText("被点击了"+count+"次");
            }
        });
        Scene scene=new Scene(pane);
        primaryStage.setTitle("Jframe");
        primaryStage.setScene(scene);
        primaryStage.show();    
    }
    public static void main(String[] args) {
        launch(args);

        }
}

这个案例显示的是会在鼠标单击后显示单击了几次.

对于想要实现双击来说,同样需要用到ActionEvent
所以双击是需要设定一个延迟时间 在延迟时间内
如果双击了 则运行双击的程序 不在运行单击的程序.

原文地址:https://www.cnblogs.com/xiaobaoa/p/12181281.html