isMetaDown方法在Java SE 14中无法正常工作,用于处理鼠标事件。

huangapple go评论166阅读模式
英文:

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生成不同的鼠标事件吗?

也许你正在寻找:

  1. SwingUtilities.isLeftMouseButton(event)
  2. SwingUtilities.isMiddleMouseButton(event)
  3. 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:

  1. SwingUtilities.isLeftMouseButton(event)
  2. SwingUtilities.isMiddleMouseButton(event)
  3. 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

huangapple
  • 本文由 发表于 2020年8月21日 09:34:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63515303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定