javafx鼠标事件中有没有能区分左右键点击的?

如题,如果有请附上代码!谢谢!... 如题,如果有请附上代码!谢谢! 展开
 我来答
百度网友12ee0fd
2013-08-31 · TA获得超过4.7万个赞
知道大有可为答主
回答量:1.1万
采纳率:66%
帮助的人:2253万
展开全部

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();
  }
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式