ismetadown方法在JavaSE14中不适用于处理鼠标事件

jjjwad0x  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(383)

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

k7fdbhmy

k7fdbhmy1#

我有一个3键鼠标,按下每个键不都会导致jvm生成不同的鼠标事件吗*
也许你在寻找: SwingUtilities.isLeftMouseButton(event) SwingUtilities.isMiddleMouseButton(event) SwingUtilities.isRightButton(event)

相关问题