有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在JavaSwing中将JPanel动态添加到另一个JPanel

我对java swing非常陌生,java也是。我使用netbeans的窗口生成器来设计GUI。我有一个名为orderListPanel的JPanel,其中包括一个名为orderListRowPanel的JPanel。现在,我得到了一个JPanel列表,我想将这些JPanel插入orderListPanel

http://postimage.org/image/ex00whf69/
OrthistListBub在中间,OrrordListWoad面板与OrthistListPosib/P>恰好相同的地方

http://postimage.org/image/dbrtn33sj/
现在我想在orderListPanel中插入许多JPanel,使其看起来像一个列表。红色方块是JPanel中的组件

我尝试使用BorderLayout,当我使用 foreach循环orderListPanel。add(List pList),我在orderListPanel中看不到任何结果。有人知道怎么解决吗


共 (1) 个答案

  1. # 1 楼答案

    不要使用Netbeans automated GUI builder。拖放式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
            }
        }
    }