我已经在JMenu上附加了一些JCheckBoxMenuItems来代替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);
}
}
}
}