1个回答
展开全部
MouseEvent 的getButton()返回一个MouseButton 枚举。
switch 判断一下
MIDDLE
Represents middle (button 2) mouse button.
NONE
Represents no button.
PRIMARY
Represents primary (button 1, usually the left) mouse button.
SECONDARY
Represents seconday (button 3, usually the right) mouse button.
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.input.*;
import javafx.scene.paint.*;
import javafx.event.*;
import javafx.application.*;
// 右手使用鼠标
public class FXMouseExample extends Application {
public static void main(String[] args) {
Application.launch(FXMouseExample.class, args);
}
@Override public void start(Stage stage) {
stage.setTitle("FX Mouse Example");
Group root = new Group();
Scene scene = SceneBuilder.create().root(root).width(400).height(300).fill(Color.WHITE)
.onMousePressed(new EventHandler<MouseEvent>(){
@Override public void handle(MouseEvent e){
MouseButton button = e.getButton();
switch(button) {
case PRIMARY: System.out.println("Left Button Pressed"); break;
case SECONDARY: System.out.println("Right Button Pressed"); break;
case MIDDLE: System.out.println("Middle Button Pressed"); break;
default:
System.out.println(button);
}
}
}).build();
stage.setScene(scene);
stage.show();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询