如何在没有setBounds的情况下将JMenuBar与JLayeredPane一起使用?

时间:2014-10-02 19:02:57

标签: jmenubar jlayeredpane

有一个简单的代码:JMenuBar和JaayeredPane中带有JBatton的JPanel。问题是JLayeredPane与JMenuBar重叠。如何解决?

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

public class Simple extends JFrame{

    public Simple() {
        super("Simple");
        setSize(1000, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        JMenuBar menu =  new JMenuBar();
        JMenu File =  new JMenu("File");
        menu.add(File);
        setJMenuBar(menu);

        JPanel panel =  new JPanel();
        panel.setSize(1000, 500);
        JButton button = new JButton("Button");
        panel.add(button);
        getLayeredPane().add(panel,JLayeredPane.DEFAULT_LAYER);
    }

    public static void main(String[] args) {
        new Simple();
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了决议。

Container conn = getContentPane();
content.setLayout(new BorderLayout());  

JLayeredPane desk =  new JDesktopPane();
desk.setOpaque(false);

conn.add(desktop, BorderLayout.CENTER);
setJMenuBar(new MainMenu());