【问题标题】:Add new fields with press of a button按下按钮添加新字段
【发布时间】:2017-08-11 08:28:11
【问题描述】:

我正在 NetBeans 中创建一个程序,用户可以在其中创建自己的 CSV 文件。我做了一个GUI。当我按下按钮时,我希望一个新的 JLabel 和一个新的 JTextField 出现在现有的下方,与按下按钮的次数一样多。我怎么做?

【问题讨论】:

    标签: java netbeans jform


    【解决方案1】:

    我做了类似的事情,我正在使用 JPanelGroupLayout 来解决这个问题,这就是我的代码:

    编辑 - 你的问题引起了我的兴趣,我已经根据你的需要修改了我的代码(当然只是基本的,你必须改进它)


    全局变量

    private GroupLayout m_layout;
    private SequentialGroup m_verticalSg;
    private ArrayList<Component> m_labelList;
    private ArrayList<Component> m_textFieldList;
    private ParallelGroup m_horizontalPgLabels;
    private ParallelGroup m_horizontalPgTextfields;
    

    方法 createLayout()

    为您的面板创建布局,其中应包含标签和文本字段组件

    private void createLayout()
    {
       m_layout = new GroupLayout(YOUR_PANEL);
       YOUR_PANEL.setLayout(m_layout);
    
       //This SequentialGroup is used for the VerticalGroup
       m_verticalSg = m_layout.createSequentialGroup();
       m_verticalSg.addContainerGap();
    
       //Two ParallelGroups are used. One for all labels and the other one for all textfields
       m_horizontalPgLabels = m_layout.createParallelGroup(GroupLayout.Alignment.LEADING);
       m_horizontalPgTextfields = m_layout.createParallelGroup(GroupLayout.Alignment.LEADING);
    
       //These component lists are used for linkSize() -> Equalize components width
       m_labelList = new ArrayList<>();
       m_textFieldList = new ArrayList<>();
    
       m_layout.setHorizontalGroup(m_layout.createParallelGroup()
                        .addGroup(m_layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(m_horizontalPgLabels)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) //Create gap between horizontal groups
                        .addGroup(m_horizontalPgTextfields)
                        .addContainerGap()));
    
       m_layout.setVerticalGroup(m_layout.createParallelGroup().addGroup(m_verticalSg.addContainerGap()));
    }
    

    方法 addNewRow()

    从您的按钮单击事件中调用此方法

    private void addNewRow()
    {
       if(m_layout == null)
          createLayout();
    
       Dimension dimLabel = new Dimension(100, 15);
       Dimension dimTextfield = new Dimension(200, 20);
    
       //Create a new label
       JLabel lbl = new JLabel();
       lbl.setText("Your text");
       lbl.setIcon(null/*Your icon*/);
       lbl.setSize(dimLabel);
       lbl.setPreferredSize(dimLabel);
    
       //Create a new textfield
       JTextField txtField = new JTextField();
       txtField.setSize(dimTextfield);
       txtField.setPreferredSize(dimTextfield);
    
       //Add components to arrays and increase index
       m_labelList.add(lbl);
       m_textFieldList.add(txtField);
    
       //Create new ParallelGroup for the vertical SequentialGroup
       ParallelGroup newVerticalParallelGroup = m_layout.createParallelGroup(GroupLayout.Alignment.LEADING);
       newVerticalParallelGroup.addComponent(lbl);
       newVerticalParallelGroup.addComponent(txtField);
       m_verticalSg.addGroup(newVerticalParallelGroup);
            m_verticalSg.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
    
       //Add the new label to the horizontal label group
       m_horizontalPgLabels.addComponent(lbl, GroupLayout.Alignment.CENTER);
       //Add the new textfield to the horizontal textfield group
       m_horizontalPgTextfields.addComponent(txtField);
    
       m_layout.linkSize(SwingConstants.HORIZONTAL, m_labelList.toArray(new Component[m_labelList.size()]));
       m_layout.linkSize(SwingConstants.HORIZONTAL, m_textFieldList.toArray(new Component[m_textFieldList.size()]));
    }
    

    最后一步是在你的按钮上添加一个ActionListener来调用方法addNewRow()

    jButton1.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
           addNewRow();
        }
    });
    

    如果有什么不清楚的地方可以随时问我。

    【讨论】:

    • 我的 YOUR_PANEL 出现错误,我应该放什么?
    • YOUR_PANEL 是一个占位符,应替换为您的面板名称。有什么问题?也许我可以帮助你
    • 1.全局变量需要在类的开头, 2. 将方法 createLayout()addNewRow() 复制到您的类中, 3. 在构造函数中将 ActionListener 添加到您的 JButton 并在此侦听器中调用方法 addNewRow()
    • 明确一点:我创建了一个包含 JButton 和 JPanel 的测试框架
    • 是的,你不知道你让我有多开心。如果我能以某种方式回报这个人情,请告诉我!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 2015-10-18
    • 2016-10-22
    • 1970-01-01
    • 2020-06-14
    • 2019-02-13
    • 1970-01-01
    相关资源
    最近更新 更多