问题
I'm using Eclipse Luna with Java 1.7 and am trying to create a JPanel
that has a JMenuBar
. This JMenuBar
contains a JMenu
which again contains a JMenuItem
. To fit the whole JMenuBar
into my project's visual design, I changed the background and foreground colors of JMenu
, JMenuBar
and JMenuItem
using UIManager.put()
. The result should be a drop-down menu with sections only divided by different shades of a Color (blue in my example). No lines or borders.
However, a whiteish, silverish etched border remains on the MenuItem
.
My question is this: How can I make this border disappear?
I'd also like to know what element this border appears on and if it's a "feature" or there's some kind of sense to it.
SSCCE:
package sscce;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
@SuppressWarnings("serial")
public class GameUIPanel extends JFrame {
public GameUIPanel() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(800, 600));
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setBackground(Color.decode("#00a2e8"));
// create and adjust Components
UIManager.put("MenuBar.background", Color.decode("#00a2e8"));
UIManager.put("MenuBar.selectionBackground", Color.decode("#0092e4"));
UIManager.put("Menu.background", Color.decode("#00beff"));
UIManager.put("Menu.selectionBackground", Color.decode("#00a2e8"));
UIManager.put("Menu.foreground", Color.decode("#91593c"));
UIManager.put("Menu.selectionForeground", Color.decode("#7e3c1a"));
UIManager.put("MenuItem.background", Color.decode("#00beff"));
UIManager.put("MenuItem.selectionBackground", Color.decode("#00a2e8"));
UIManager.put("MenuItem.foreground", Color.decode("#91593c"));
UIManager.put("MenuItem.selectionForeground", Color.decode("#7e3c1a"));
UIManager.put("PopupMenu.border",
BorderFactory.createLineBorder(Color.decode("#00beff"), 1));
UIManager.put("PopupMenu.foreground", Color.decode("#000000"));
JMenuBar menubar = new JMenuBar();
menubar.setBorder(BorderFactory.createLineBorder(
Color.decode("#7e3c1a"), 2, false));
JMenu gameMenu = new JMenu("Game");
gameMenu.setOpaque(true);
gameMenu.setFont(Font.decode("Arial-BOLD-24"));
JMenuItem back = new JMenuItem("Back");
back.setFont(Font.decode("Arial-BOLD-24"));
gameMenu.add(back);
menubar.add(gameMenu);
setJMenuBar(menubar);
pack();
setVisible(true);
}
public static void main(String[] args) {
GameUIPanel gui = new GameUIPanel();
}
}
Added an image, as requestet. I hope this helps.
来源:https://stackoverflow.com/questions/28768587/how-to-remove-the-border-on-a-jmenu-or-jmenuitem