【问题标题】:Dynamic adding JPanel to another JPanel in Java Swing在 Java Swing 中将 JPanel 动态添加到另一个 JPanel
【发布时间】:2013-02-14 03:26:33
【问题描述】:

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

http://postimage.org/image/ex00whf69/
orderListPanel 在中间,orderListRowPanel 和 orderListPanel 一样

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

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

【问题讨论】:

    标签: java swing layout dynamic netbeans


    【解决方案1】:

    不要使用 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
            }
        }
    }
    

    【讨论】:

    • 非常感谢!!在我将布局设置为 BoxLayout 后,它工作得很好。我真的很感激。我将开始学习 swing 的基础知识,而不是使用 windows builder。
    • @user2117945:不客气。如果我的回答对您有帮助,请点击我的问题左侧的“右”标记将此标记为答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多