我已经将一些JCheckBoxMenuItems附加到我的JMenu而不是JMenuItem.当用户单击JMenu时,它将显示JCheckBoxMenuItems.一旦用户从JCheckBoxMenuItem中选择了一个框,JCheckBoxMenuItems列表就会消失,即关闭.如何覆盖默认操作以使其保持打开状态(这样他们可以一次选择/取消选择多个框),直到用户点击JCheckBoxMenuItems之外的某个位置?
我看到让这个工作的两个基本方法是提供你自己的UI委托,这不是很漂亮,并且需要你为你想要支持的每个平台提供UI委托或覆盖processMouseEvent
它JMenuItem
(或者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); } } } }