在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
所以雙擊是需要設定一個延遲時間 在延遲時間內
如果雙擊了 則運行雙擊的程序 不在運行單擊的程序.