2012-06-19 35 views
1

我想知道我們是否可以有一個JPanel,其佈局不是它的父JFrame。例如。如果我有帶邊框佈局的JFrame,並且我們有一個嵌入的JPanel,並且它有不同的佈局。可能嗎 ?JPanel具有不同的佈局,它的JFrame

我正在努力做到這一點。但是這樣JPanel的組件沒有顯示出來。

這裏來詳細的問題:我有一個JFrame和佈局是邊框式佈局

。我在這個框架上添加一個JPanel。如果我不爲JPanel設置任何佈局。 JPanel的所有組件都顯示在窗口上,但是當我爲JPanel設置網格佈局時,JPanel的組件不可見。我將Layout添加到JPanel以便對齊組件。以下是我的代碼:

我有一個主類,一個框架類和Jpanel類。

public class AppMain { 

    public static void main(String[] args) { 

     AppPage1 page1 = new AppPage1("test"); 
     page1.setVisible(true); 
    } 
} 



public class AppPage1 extends JFrame { 

    public AppPage1(String title) throws HeadlessException { 

     super(title); 
     this.setLayout(new BorderLayout()); 

     addWindowListener(new WindowAdapter() { 

      public void windowOpened(WindowEvent e) { 
       setExtendedState(MAXIMIZED_BOTH); 
      } 
     }); 

     //Panel for logo 
     JLabel testLogo = new JLabel(""); 
     testLogo.setIcon(new javax.swing.ImageIcon("test.JPG")); 
     List<JComponent> componentList = new ArrayList<JComponent>(); 
     componentList.add(testLogo); 


     PagePanel logoPanel = new PagePanel(componentList,null); 
     this.add(logoPanel, BorderLayout.NORTH); 


     //Panel for Button and checkboxes 
     JLabel panelTitle = new JLabel("test Wizard"); 
     JRadioButton rdButton_ExistingConfigurationFile = new JRadioButton("Existing Configuration File"); 
     JRadioButton rdButton_ConfigureNewPropertyFile = new JRadioButton("Configure new Property File"); 

     componentList = new ArrayList<JComponent>(); 
     componentList.add(panelTitle); 
     componentList.add(rdButton_ExistingConfigurationFile); 
     componentList.add(rdButton_ConfigureNewPropertyFile); 

     PagePanel buttonPanel = new PagePanel(componentList,new GroupLayout(this)); 
     this.add(buttonPanel, BorderLayout.CENTER); 

     this.pack(); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     validate(); 
    } 
} 



    public class PagePanel extends JPanel { 

    public PagePanel(List<JComponent> componentList, LayoutManager layOutManager) { 

     this.setBackground(Color.decode("#4E6987")); 

     if (layOutManager != null) { 
      this.setLayout(null); 
     } 
     for (JComponent jComponent : componentList) { 

      jComponent.setBackground(Color.decode("#4E6987")); 
      this.add(jComponent); 
     } 
    } 
} 

由於提前 拉維·庫馬爾

+0

是的,這是可能的。雖然爲什麼它不會發生在你身邊,爲此你需要準確地展示你在做什麼!這將有助於任何人回答你,所以你的一些代碼將非常感激。默認情況下'JFrame具有BorderLayout'和'JPanel具有FLowLayout',所以不需要做任何事情,只需在你的'JPanel'上添加組件並將其添加到你的'JFrame'中而不需要其他任何行,你將得到「YES」作爲答案您的問題:-) –

+0

當然,您可以爲層次結構的每個組件分配不同的佈局管理器。默認情況下,JPanel使用FlowLayout。如果遇到麻煩,請發佈[SSCCE](http://sscce.org)以獲得更好的幫助。 –

回答

0

問題是你設置了一個空佈局管理器,而不是在你的PagePanel類中設置佈局管理器。

if (layOutManager != null) { 
    this.setLayout(null); 
} 

如果設置了null佈局,你必須位置和大小的組件「手動」,所以要儘量避免儘可能多地和使用適當LayoutManager的。

此行沒有任何意義之一:

PagePanel buttonPanel = new PagePanel(componentList,new GroupLayout(this)); 

的GroupLayout的構造函數的參數應該是在其上設置的佈局,而不是一個隨機分量的容器。

+0

糟糕!我沒有注意到這一點。這是我犯的最愚蠢的錯誤之一。感謝您糾正它。 –

0

我可以寫一個例子證明這是完全可能的,但SO已經有一個偉大的「嵌套佈局」例如,所以a link就足夠了