2014-10-02 48 views
0

有一個簡單的代碼:JMenuBar和JaayeredPane中JBatton的JPanel。問題是JLayeredPane與JMenuBar重疊。如何解決它?如何在沒有setBounds的情況下使用帶有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(); 
    } 
} 

回答

0

我罰款了決議。

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

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

conn.add(desktop, BorderLayout.CENTER); 
setJMenuBar(new MainMenu()); 
相關問題