我正在用Swing做这个图形界面.我遇到的问题是我不能采用JMenuItems周围的白色边框,并将其全部涂成黑色.这是一张图片:
我想像这样画(我用油漆编辑了图像:D):
有人能帮助我吗?我会感激任何帮助.谢谢!
我只是做了这个快速测试,使用
UIManager.put("PopupMenu.border", new LineBorder(Color.RED));
import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.LineBorder; 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(); } UIManager.put("PopupMenu.border", new LineBorder(Color.RED)); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("Stuff"); menu.add(new JMenuItem("A")); menu.add(new JMenuItem("BB")); menu.add(new JMenuItem("CCC")); menuBar.add(menu); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.setJMenuBar(menuBar); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } } }
现在,这可能不太理想,因为这会影响程序中的所有弹出菜单
更新
我查看JMenu
并通过它的UI委托,看起来弹出菜单是在一个被调用的private
方法中创建的,这将是一个注入自定义代码的好地方.JMenu
ensurePopupMenuCreated
实际上,该方法在许多不同的地方被调用,但可能getPopupMenu
是最容易访问的
import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.KeyStroke; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.Border; import javax.swing.border.LineBorder; 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 menuBar = new JMenuBar(); JMenu menu = new JMenu("Stuff") { private Border border = new LineBorder(Color.RED); @Override public JPopupMenu getPopupMenu() { JPopupMenu menu = super.getPopupMenu(); menu.setBorder(border); return menu; } }; JMenuItem mi = new JMenuItem("Help", 'H'); mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.META_MASK)); menu.add(new JMenuItem("A")); menu.add(new JMenuItem("BB")); menu.add(new JMenuItem("CCC")); menu.add(mi); menuBar.add(menu); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.setJMenuBar(menuBar); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } } }