哪个鼠标键是中间的? [英] Which mouse button is the middle one?
问题描述
我目前正在开发一个Java程序,只有当用户点击左键和右键单击按钮时才能触发某个事件。
I'm currently developing a program in Java where a certain event must be triggered only when the user clicks with both the left and the right click on a button.
由于它有点不同寻常,我决定先测试一下。这是:
Since it is a little unconventional, I decided to first test this. Here it is:
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 (" ");
}
}
});
}
}
我测试了它并且它有效,但有一个问题。
I tested it and it works, but there is a problem.
如您所见,鼠标左键由 MouseEvent.BUTTON1
和鼠标右键表示通过 MouseEvent.BUTTON3
。
As you can see, the left mouse button is represented by MouseEvent.BUTTON1
and the right mouse button by MouseEvent.BUTTON3
.
如果用户的鼠标没有滚轮(显然是这样的话)鼠标仍然存在),然后在MouseEvent中只设置了两个按钮。这是否意味着右键将由 MouseEvent.BUTTON2
而不是 MouseEvent.BUTTON3
表示?如果是,我如何更改我的代码以适应这个?有什么方法可以检测到这样的东西吗?
If the user has a mouse which doesn't have a scroll wheel (apparently such mice still exist), then only two buttons are set in MouseEvent. Does that mean that the right button will be represented by MouseEvent.BUTTON2
instead of MouseEvent.BUTTON3
? If yes, how can I change my code to accomodate this? Is there any way I can detect something like this?
我读了我在MouseListener接口和MouseEvent上可以找到的任何东西,但是我找不到关于这个的东西。
I read anything I could find on the MouseListener interface and on MouseEvent, but I couldn't find something about this.
推荐答案
要确定按下哪个鼠标按钮,这三个方法来自SwingUtilities 可以帮助您:
To determine which of the Mouse buttons is pressed, these three methods from SwingUtilities could help you:
- isLeftMouseButton
- isMiddleMouseButton
- isRightMouseButton
这篇关于哪个鼠标键是中间的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!