单击后如何防止 JCheckBoxMenuItem 关闭?

How to prevent JCheckBoxMenuItem from closing once clicked?

我已将一些 JCheckBoxMenuItems 附加到我的 JMenu 以代替 JMenuItem。当用户单击 JMenu 时,它会显示 JCheckBoxMenuItems。一旦用户从 JCheckBoxMenuItem 中选择了其中一个框,JCheckBoxMenuItems 列表就会消失,即关闭。在用户单击 JCheckBoxMenuItems 之外的某处之前,我如何覆盖此默认操作以使其保持打开状态(这样他们可以 select/deselect 一次多个框)?

我见过的使它起作用的两种基本方法是提供您自己的 UI 委托,这不太好,需要您提供 UI 委托您想要支持或覆盖 JMenuItemprocessMouseEvent(或您的情况下的 JCheckBoxMenuItem)的每个平台。

例如...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JMenuBar mb = new JMenuBar();
                JMenu menu = new JMenu("Lots-o-stuff");
                mb.add(menu);

                menu.add(new MyMenuItem("Apples"));
                menu.add(new MyMenuItem("Pears"));
                menu.add(new MyMenuItem("Bananas"));

                JFrame frame = new JFrame("Testing");
                frame.setJMenuBar(mb);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

    public class MyMenuItem extends JCheckBoxMenuItem {

        public MyMenuItem() {
        }

        public MyMenuItem(Icon icon) {
            super(icon);
        }

        public MyMenuItem(String text) {
            super(text);
        }

        public MyMenuItem(Action a) {
            super(a);
        }

        public MyMenuItem(String text, Icon icon) {
            super(text, icon);
        }

        public MyMenuItem(String text, boolean b) {
            super(text, b);
        }

        public MyMenuItem(String text, Icon icon, boolean b) {
            super(text, icon, b);
        }

        @Override
        protected void processMouseEvent(MouseEvent evt) {
            if (evt.getID() == MouseEvent.MOUSE_RELEASED && contains(evt.getPoint())) {
                doClick();
                setArmed(true);
            } else {
                super.processMouseEvent(evt);
            }
        }
    }

}