有一个简单的代码:JMenuBar和JLayeredPane中带有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();
}
}
我对解析进行了定义。
Container conn = getContentPane();
content.setLayout(new BorderLayout());
JLayeredPane desk = new JDesktopPane();
desk.setOpaque(false);
conn.add(desktop, BorderLayout.CENTER);
setJMenuBar(new MainMenu());