2013-02-28 123 views
1

我對Java swing很新穎,java也是如此。我使用netbeans的窗口構建器來設計GUI。我有一個名爲orderListPanel的JPanel,它包含一個名爲orderListRowPanel的JPanel。現在,我得到了一個JPanel列表,並且我想將這些JPanels插入到orderListPanel中。將Java JPanel動態添加到Java中的另一個JPanel Swing

http://postimage.org/image/ex00whf69/
orderListPanel是在中間,orderListRowPanel是一樣的地方像orderListPanel

http://postimage.org/image/dbrtn33sj/
現在我想插入許多JPanels到orderListPanel,使它看起來像一個列表。紅色正方形是JPanel側面的組件。

我嘗試使用BorderLayout,當我使用 foreach循環orderListPanel.add(List pList)時,我看不到orderListPanel中的任何結果。有人知道如何解決它嗎?

回答

2

請勿使用Netbeans自動GUI構建器。在Java社區中拖放GUI構建技術是不可接受的。您尚未提供代碼,因此我們無法進行代碼編輯。您的圖片不可用。

但是,這是你如何做到的。這是一個純手工代碼

import java.awt.*; 
import java.util.ArrayList; 
import javax.swing.*; 
import java.util.List; 

public class GUIBuilder extends JFrame 
{ 
    private JPanel orderList; 
    private JPanel orderListRow; 
    private JPanel additionalPanel; 

    private List panels = new ArrayList(); //Your List 

    private JLabel label1, label2, label3; 

    public GUIBuilder() 
    { 
     label1 = new JLabel("Label 1"); //Create the JLabels 
     label2 = new JLabel("Label 2");//Create the JLabels 
     label3 = new JLabel("Label 3");//Create the JLabels 


     orderList = new JPanel(); //Creating the orderList JPanel 
     orderList.setLayout(new BoxLayout(orderList, BoxLayout.Y_AXIS)); //Setting Box layout, and set the direction to Y axis. 


     orderListRow = new JPanel(); //Creating the orderListRow JPanel   
     orderListRow.add(label1); 

     additionalPanel = new JPanel(); //Creating the additionalPanel JPanel  
     additionalPanel.add(label2); 

     orderList.add(orderListRow); //Adding orderListRow into orderList 
     orderList.add(additionalPanel); //Adding additionalPanel into orderList 

     this.setLayout(new GridLayout(1,1)); 
     this.add(orderList); //Setting orderList into JFrame 

     this.pack(); //Setting JFrame size. This will only take required space 
     this.setVisible(true); //Making JFrame Visible 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //When you hit the 'X' button, the program will exit 
    } 

    public static void main(String[]args) 
    { 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //Setting the UI into your native platform UI 
      new GUIBuilder(); //Calling your program 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); //If any error occured in setting up UI, print the stack trace 
     } 
    } 
} 

如果你有一個列表裏面的面板,然後用這個

import java.awt.*; 
import java.util.ArrayList; 
import javax.swing.*; 
import java.util.List; 

public class GUIBuilder extends JFrame 
{ 
    private JPanel orderList; 
    private JPanel orderListRow; 
    private JPanel additionalPanel; 

    private List<JPanel> panels = new ArrayList<JPanel>(); //Your List 

    private JLabel label1, label2, label3; 

    public GUIBuilder() 
    { 
     label1 = new JLabel("Label 1"); //Create the JLabels 
     label2 = new JLabel("Label 2");//Create the JLabels 
     label3 = new JLabel("Label 3");//Create the JLabels 


     orderList = new JPanel(); //Creating the orderList JPanel 
     orderList.setLayout(new BoxLayout(orderList, BoxLayout.Y_AXIS)); //Setting Box layout, and set the direction to Y axis. 


     orderListRow = new JPanel(); //Creating the orderListRow JPanel   
     orderListRow.add(label1); 
     panels.add(orderListRow); // Add the panel to the List 

     additionalPanel = new JPanel(); //Creating the additionalPanel JPanel  
     additionalPanel.add(label2); 
     panels.add(additionalPanel); // Add the panel to the List 


     for(int i=0;i<panels.size();i++) 
     { 
      orderList.add(panels.get(i)); 
     } 



     this.setLayout(new GridLayout(1,1)); 
     this.add(orderList); //Setting orderList into JFrame 

     this.pack(); //Setting JFrame size. This will only take required space 
     this.setVisible(true); //Making JFrame Visible 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //When you hit the 'X' button, the program will exit 
    } 

    public static void main(String[]args) 
    { 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //Setting the UI into your native platform UI 
      new GUIBuilder(); //Calling your program 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); //If any error occured in setting up UI, print the stack trace 
     } 
    } 
} 
+0

真的謝謝!!得多將佈局設置爲BoxLayout之後,它的效果非常好。我真的很感激。我將開始學習swing的基礎,而不是使用windows builder。 – user2117945 2013-03-02 00:36:57

+0

@ user2117945:不客氣。如果我的回答對您有幫助,請點擊我問題左側的「右側」標記,將其標記爲答案:) – 2013-03-02 04:37:37

+0

爲什麼不使用netbeans編輯器? – claudio495h 2015-02-19 20:02:40