我的 Java JMenu 出现问题。当我添加子菜单时,子菜单中的蓝色条与菜单重叠。(如图所示)我不知道如何解决这个问题,而且我在互联网上找不到任何内容。 我希望你能帮助我。
示例图片:
PS:我使用 Windows 10 外观
public JFrame frame;
public static JDesktopPane desktopPane = new JDesktopPane();
/**
* Create the application.
*/
public Systemstammdaten() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
desktopPane.setBounds(0, 22, frame.getWidth(), frame.getHeight());
}
});
frame.getContentPane().setBackground(Color.LIGHT_GRAY);
frame.setBounds(100, 100, 895, 593);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(null);
JMenuBar menuBar = new JMenuBar();
menuBar.setBounds(0, 0, 3860, 21);
frame.getContentPane().add(menuBar);
JMenu mnBearbeiten = new JMenu("Bearbeiten");
menuBar.add(mnBearbeiten);
JMenuItem mntmNutzer = new JMenuItem("Nutzer");
mntmNutzer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Nutzer iframe = new Nutzer();
iframe.setVisible(true);
desktopPane.add(iframe);
iframe.setSelected(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
mnBearbeiten.add(mntmNutzer);
JMenuItem mntmRechte = new JMenuItem("Rechte");
mnBearbeiten.add(mntmRechte);
JMenu mnNewMenu = new JMenu("New menu");
mnBearbeiten.add(mnNewMenu);
JMenuItem mntmNewMenuItem = new JMenuItem("New menu item");
mnNewMenu.add(mntmNewMenuItem);
JMenuItem mntmNewMenuItem_1 = new JMenuItem("New menu item");
mnNewMenu.add(mntmNewMenuItem_1);
desktopPane.setBackground(Color.LIGHT_GRAY);
desktopPane.setBounds(0, 22, 879, 532);
frame.getContentPane().add(desktopPane);
}
最佳答案
frame.getContentPane().add(menuBar);
您应该使用:
frame.setJMenuBar( menuBar );
将菜单栏添加到框架的特殊保留区域。
另外,不要使用 null 布局和 setBounds(...)。 Swing 被设计为与布局管理器一起使用。
然后,您只需将桌面 Pane 添加到框架中,它将占用菜单栏未占用的框架空间。
阅读Swing Tutorial 。以下部分:
如何使用菜单栏
如何使用 JDesktopPane
包含更多信息和工作示例以帮助您入门。
关于java - 为什么蓝色条在菜单中重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53574607/