2013-02-28 1 views
1

저는 자바 스윙에 매우 익숙하며 자바도 그렇습니다. netbeans의 창 빌더를 사용하여 GUI를 디자인합니다. orderListRowPanel이라는 JPanel을 포함하는 orderListPanel이라는 JPanel이 있습니다. 이제 JPanel 목록을 얻었고이 JPanels를 orderListPanel에 삽입하려고합니다.JPanel을 Java의 다른 JPanel에 동적으로 추가 스윙

http://postimage.org/image/ex00whf69/
orderListPanel 중간에 있고 orderListRowPanel 지금 내가 orderListPanel에 많은 JPanel의를 삽입하고이 목록처럼 보이게 만들고 싶어 orderListPanel

처럼

http://postimage.org/image/dbrtn33sj/
그냥 같은 장소입니다. 빨간색 사각형은 JPanel의 구성 요소입니다.

BorderLayout을 사용하려고하고 foreach 루프 orderListPanel.add (List pList)를 사용할 때 orderListPanel 내부에 결과가 표시되지 않습니다. 아무도 그것을 해결하는 방법을 알고 있습니까?

답변

2

Netbeans 자동 GUI 빌더를 사용하지 마십시오. 드래그 & 드롭 GUI 구축 기술은 Java 커뮤니티에서 허용되지 않습니다. 코드를 제공하지 않아 코드 편집을 할 수 없습니다. 이미지를 사용할 수 없습니다.

그러나 이렇게하면 어떻게 할 수 있습니다. 이것은 순수한 손 코드

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으로 설정하면 매우 효과적입니다. 정말 고맙습니다. 창 빌더를 사용하는 대신 스윙의 기초를 배우기 시작할 것입니다. – user2117945

+0

@ user2117945 : 환영합니다. 내 대답이 도움이 되었다면, 내 질문의 왼쪽에있는 "오른쪽"표시를 클릭하여 대답을 표시하십시오 :) –

+0

왜 netbeans 편집기를 사용하지 않습니까? – claudio495h

관련 문제