java哪个按钮鼠标_java – 哪个鼠标按钮是中间的?

我正在Java开发一个程序,只有当用户单击左键和右键单击按钮时,必须触发某个事件。

由于这是一个非常规的,我决定先测试一下。这里是:

import javax.swing.JFrame;

import javax.swing.JButton;

import javax.swing.JLabel;

import java.awt.event.MouseListener;

import java.awt.event.MouseEvent;

public class GUI

{

private JFrame mainframe;

private JButton thebutton;

private boolean left_is_pressed;

private boolean right_is_pressed;

private JLabel notifier;

public GUI ()

{

thebutton = new JButton ("Double Press Me");

addListen ();

thebutton.setBounds (20, 20, 150, 40);

notifier = new JLabel (" ");

notifier.setBounds (20, 100, 170, 20);

mainframe = new JFrame ("Double Mouse Tester");

mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);

mainframe.setResizable (false);

mainframe.setSize (400, 250);

mainframe.setLayout (null);

mainframe.add (thebutton);

mainframe.add (notifier);

mainframe.setVisible (true);

left_is_pressed = right_is_pressed = false;

}

private void addListen ()

{

thebutton.addMouseListener (new MouseListener ()

{

@Override public void mouseClicked (MouseEvent e) { }

@Override public void mouseEntered (MouseEvent e) { }

@Override public void mouseExited (MouseEvent e) { }

@Override public void mousePressed (MouseEvent e)

{

//If left button pressed

if (e.getButton () == MouseEvent.BUTTON1)

{

//Set that it is pressed

left_is_pressed = true;

if (right_is_pressed)

{

//Write that both are pressed

notifier.setText ("Both pressed");

}

}

//If right button pressed

else if (e.getButton () == MouseEvent.BUTTON3)

{

//Set that it is pressed

right_is_pressed = true;

if (left_is_pressed)

{

//Write that both are pressed

notifier.setText ("Both pressed");

}

}

}

@Override public void mouseReleased (MouseEvent e)

{

//If left button is released

if (e.getButton () == MouseEvent.BUTTON1)

{

//Set that it is not pressed

left_is_pressed = false;

//Remove notification

notifier.setText (" ");

}

//If right button is released

else if (e.getButton () == MouseEvent.BUTTON3)

{

//Set that it is not pressed

right_is_pressed = false;

//Remove notification

notifier.setText (" ");

}

}

});

}

}

我测试它,它的工作,但有一个问题。

您可以看到,鼠标左键由MouseEvent.BUTTON1和MouseEvent.BUTTON3的鼠标右键代表。

如果用户有一个没有滚轮的鼠标(显然这样的鼠标仍然存在),那么在MouseEvent中只有两个按钮被设置。这是否意味着右键会被MouseEvent.BUTTON2代替,而不是MouseEvent.BUTTON3?如果是,如何更改我的代码以适应这一点?有什么办法可以检测到这样的东西吗?

我在MouseListener接口和MouseEvent上看到任何可以找到的内容,但是我找不到关于这个的一些东西。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值