英文:
isMetaDown method not working in java SE 14 for handling mouse event
问题
扩展JFrame的类主体
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MouseDetailsFrame extends JFrame {
private final JLabel statusJLabel;
public MouseDetailsFrame() {
super("Mouse Adapter example");
statusJLabel = new JLabel("You clicked 0 times");
add(statusJLabel, BorderLayout.SOUTH);
addMouseListener(new MouseAdapterHandler());
}
private class MouseAdapterHandler extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent event) {
int posX = event.getX();
int posY = event.getY();
String details = String.format("You clicked %d time(s) ", event.getClickCount());
if (event.isMetaDown() == true)
details += "with Right Mouse Button";
else if (event.isAltDown() == true)
details += "with center Mouse Button";
else
details += "with Left Mouse Button";
statusJLabel.setText(details + String.format(" at ( %d, %d )", posX, posY));
}
}
}
包含主方法的驱动程序类:
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.JFrame;
public class MouseEventTest {
public static void main(String[] args) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
MouseDetailsFrame mouseDetailsFrame = new MouseDetailsFrame();
mouseDetailsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mouseDetailsFrame.setSize(400, 200);
mouseDetailsFrame.setVisible(true);
}
}
每当我按任何鼠标按钮时,程序只注册为左按钮。
但是,可以通过同时按住Alt键并按下左鼠标按钮来模拟中间鼠标按钮。
我不太明白,我有一个3按钮鼠标,按下每个按钮不会导致JVM生成不同的鼠标事件吗?
英文:
Body of class that extends JFrame
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MouseDetailsFrame extends JFrame {
private final JLabel statusJLabel;
public MouseDetailsFrame() {
super("Mouse Adapter example");
statusJLabel = new JLabel("You clicked 0 times");
add(statusJLabel, BorderLayout.SOUTH);
addMouseListener(new MouseAdapterHandler());
}
private class MouseAdapterHandler extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent event) {
int posX = event.getX();
int posY = event.getY();
String details = String.format("You clicked %d time(s) ", event.getClickCount());
if (event.isMetaDown() == true)
details += "with Right Mouse Button";
else if (event.isAltDown() == true)
details += "with center Mouse Button";
else
details += "with Left Mouse Button";
statusJLabel.setText(details + String.format(" at ( %d, %d )", posX, posY));
}
}
}
Driver class containing main method:
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.JFrame;
public class MouseEventTest {
public static void main(String[] args) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
MouseDetailsFrame mouseDetailsFrame = new MouseDetailsFrame();
mouseDetailsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mouseDetailsFrame.setSize(400, 200);
mouseDetailsFrame.setVisible(true);
}
}
Whenever I press any mouse button the program registers only as a Left button.
However, the center mouse button can be simulated by pressing left mouse button while holding alt key.
I don't quite understand, I have a 3 button mouse, doesn't pressing each button causes JVM to generate different mouse events?
答案1
得分: 2
我有一个有3个按钮的鼠标,按下每个按钮会导致JVM生成不同的鼠标事件吗?
也许你正在寻找:
SwingUtilities.isLeftMouseButton(event)
SwingUtilities.isMiddleMouseButton(event)
SwingUtilities.isRightButton(event)
英文:
> I have a 3 button mouse, doesn't pressing each button causes JVM to generate different mouse events?*
Maybe you are looking for:
SwingUtilities.isLeftMouseButton(event)
SwingUtilities.isMiddleMouseButton(event)
SwingUtilities.isRightButton(event)
答案2
得分: 0
在旧版本的Java中,如果按下Alt键或按下中间按钮,event.isAltDown()
会返回true,这是一个错误。
在新版本中,比如Java 14,只有在按下Alt键时才会返回true。
同样的错误也影响了event.isMetaDown()
,但是是针对鼠标的右键。
换句话说,不应该使用event.isAltDown()
和event.isMetaDown()
来测试鼠标按钮。
英文:
In older Java versions event.isAltDown()
returns true if the Alt key is pressed or if the middle button is pressed, this is a bug.
In newer versions, like Java 14, it returns true only if the Alt key is pressed.
The same bug affects event.isMetaDown()
but with the right mouse button.
In other words, event.isAltDown()
and event.isMetaDown()
should not be used to test for mouse buttons
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论