我正在用 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 委托(delegate),似乎弹出菜单是在名为 private
的 JMenu
的 ensurePopupMenuCreated
方法中创建的,这本来是注入(inject)自定义代码的绝佳位置。该方法实际上在许多不同的地方被调用,但可能
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);
}
}
}
关于java - 如何去除 JMenu 和 JMenuItems 中的白色边框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34458735/