Java 右键单击 JButton
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2006188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Right click on JButton
提问by I82Much
I am trying to write a Minesweeper clone in Java for fun. I have a grid of JButtons whose labels I will change to represent the danger count, flags, etc.
为了好玩,我正在尝试用 Java 编写一个扫雷器克隆。我有一个 JButton 网格,我将更改其标签以表示危险计数、标志等。
My problem is, I don't know how to get a right click on a JButton to depress the button. I've done the following:
我的问题是,我不知道如何右键单击 JButton 以按下按钮。我做了以下工作:
button.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
boolean mine = field.isMine(x, y);
if (e.isPopupTrigger()) {
button.setText("F");
}
else {
if (mine) {
button.setText("X");
}
}
}
});
This doesn't seem to be working at all; the "F" is never shown, only the "X" part. But more importantly, this does nothing for depressing the button.
这似乎根本不起作用;“F”从不显示,只显示“X”部分。但更重要的是,这对按下按钮没有任何作用。
EDIT: Macs have popup trigger happen on mousePress, not mouseClick.
编辑:Mac 有弹出触发发生在 mousePress 上,而不是 mouseClick。
EDIT: Here's the solution I worked out based off of accepted answer:
编辑:这是我根据接受的答案制定的解决方案:
button.addMouseListener(new MouseAdapter(){
boolean pressed;
@Override
public void mousePressed(MouseEvent e) {
button.getModel().setArmed(true);
button.getModel().setPressed(true);
pressed = true;
}
@Override
public void mouseReleased(MouseEvent e) {
//if(isRightButtonPressed) {underlyingButton.getModel().setPressed(true));
button.getModel().setArmed(false);
button.getModel().setPressed(false);
if (pressed) {
if (SwingUtilities.isRightMouseButton(e)) {
button.setText("F");
}
else {
button.setText("X");
}
}
pressed = false;
}
@Override
public void mouseExited(MouseEvent e) {
pressed = false;
}
@Override
public void mouseEntered(MouseEvent e) {
pressed = true;
}
});
add(button);
采纳答案by Rastislav Komara
Button can't be pressed by right click. Add such a lines to you mouse listener
不能通过右键单击按钮。将这样的行添加到您的鼠标侦听器
mousePressed:
鼠标按下:
if(isRightButtonPressed) {underlyingButton.getModel().setPressed(true));
mouseReleased:
鼠标释放:
if(needReset) {underlyingButton.getModel().setPressed(false));
or do there whatever want.
或在那里做任何想做的事。
回答by nc3b
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/MouseEvent.html
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/MouseEvent.html
MouseEvent has some properties
MouseEvent 有一些属性
static int BUTTON1
static int BUTTON2
static int BUTTON3
among others. Check those when your event fires.
其中。当您的事件触发时检查这些。
EDIT
编辑
public int getButton()
公共 int getButton()
Returns which, if any, of the mouse buttons has changed state.
回答by Carl Smotricz
The button being visibly depressed on right click isn't part of the "normal" behavior of buttons. You may be able to fake it using JToggleButton
s, or simply changing the button's background color and maybe border while the right mouse button is being held down.
右键单击时明显按下的按钮不是按钮“正常”行为的一部分。您可以使用JToggleButton
s来伪造它,或者在按住鼠标右键时简单地更改按钮的背景颜色和边框。
回答by perdian
I wouldn't use isPopupTrigger
but directly check for the right button:
我不会使用isPopupTrigger
但直接检查正确的按钮:
button.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e){ boolean mine = field.isMine(x, y); if (e.getButton() == MouseEvent.BUTTON2) { button.setText("F"); } ...
回答by Timothy
If you are certain that the event is properly being triggered (debug FTW!) and that the button.setText("F") is happening, then perhaps you simply need to repaint.
如果您确定事件被正确触发(调试 FTW!)并且 button.setText("F") 正在发生,那么也许您只需要重新绘制。
Repaint the button. http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#repaint(java.awt.Rectangle)
重新绘制按钮。 http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#repaint(java.awt.Rectangle)
回答by sateesh
As you have mentioned that checking for "mousePressed" solved your issue. And the Javadoc of isPopupTriggerwould explain the need for this:
正如您所提到的,检查“mousePressed”解决了您的问题。isPopupTrigger的 Javadoc将解释对此的需要:
public boolean isPopupTrigger()
...
Note: Popup menus are triggered differently on different systems. Therefore, isPopupTrigger should be checked in both mousePressed and mouseReleased for proper cross-platform functionality.
public boolean isPopupTrigger()
...
注意:弹出菜单在不同系统上的触发方式不同。因此,应该在 mousePressed 和 mouseReleased 中检查 isPopupTrigger 以获得正确的跨平台功能。
Also see the section on The Mouse Listener APIin the Java Swingtutorial.
另请参阅Java Swing教程中有关鼠标侦听器 API的部分。
回答by Artur
This works for me fine on Mac:
这对我来说在 Mac 上很好用:
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest extends JFrame {
JButton button;
public ButtonTest() {
button = new JButton("W");
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == 3) { // if right click
button.setText("F");
button.getModel().setPressed(false);
// button.setEnabled(true);
} else {
button.setText("X");
button.getModel().setPressed(true);
// button.setEnabled(false);
}
}
});
this.add(button);
this.setVisible(true);
}
public static void main(String[] args) {
new ButtonTest();
}
}
You might as well check for e.getButton() == 2 but I don't know when this one is triggered on Macs.
您不妨检查一下 e.getButton() == 2 但我不知道何时在 Mac 上触发了这个。
回答by Eugene Ryzhikov
Just a small addition: the simplest way to check for the right button is SwingUtilities.isRightMouseButton
只是一个小小的补充:检查右侧按钮的最简单方法是 SwingUtilities.isRightMouseButton