一:双击事件,和检测键盘按键
package fx.com;
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.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class Main extends Application{
public static void main(String[] args) {
// TODO 自动生成的方法存根
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Group group=new Group();
//单机,双击,监听键盘事件
Button b1=new Button();
b1.setText("b1_按钮单击");
Button b2=new Button();
b2.setText("b2_鼠标击");
b2.setLayoutY(30);
Button b3=new Button();
b3.setText("b3_键盘按下(英文键盘)");
b3.setLayoutY(60);
b1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// TODO 自动生成的方法存根
System.out.println("button_单击事件");
}
});
b2.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
// TODO 自动生成的方法存根
String but_name=event.getButton().name();
if(event.getClickCount()==2) {
System.out.println("你用按键:“"+but_name+"”>>双击2次");
}else if(event.getClickCount()==1){
System.out.println("你用按键:“"+but_name+"”>>点击事件");
}else{
System.out.println("你用按键:“"+but_name+"”>>多击事件");
}
}
});
b3.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
// TODO 自动生成的方法存根
System.out.println("按键按下:>"+event.getCode().getName());
}
});
b3.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
// TODO 自动生成的方法存根
System.out.println("按键放下:>"+event.getCode().getName());
}
});
group.getChildren().addAll(b1,b2,b3);
Scene scene=new Scene(group);
primaryStage.setTitle("Button_单击,双击,监听键盘事件");
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.setScene(scene);
primaryStage.show();
}
}