我想通过单击添加按钮动态添加JLabel和文本框 [英] I want to add a JLabel and Text box dynamically by clicking add button

查看:432
本文介绍了我想通过单击添加按钮动态添加JLabel和文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我动态创建文本和标签框时,它应采用文本框:标签框"的格式,然后当我再次单击添加"按钮时,下一行应重复相同的图案,依此类推...我应该使用哪种布局使用以及如何使用?

When i create the text and label box dynamically it should sit in the format of "Textbox: Labelbox" then when i click on add button again the same pattern should repeat on next line and so on... Which layout should i use and how ?

这是我使用的代码

if(field_name.getText().equals("")){  
            error.setForeground(Color.red);    
            error.setText("Enter the Field name first");
        } else {  
        JLabel l = new JLabel(field_name.getText(), JLabel.RIGHT);   
        JTextField textField = new JTextField();  
        Dimension dim = new Dimension(20,30);  
        textField.setPreferredSize(dim);  
        field_layer.add(l);  
        field_layer.add(textField);  
        SpringUtilities.makeCompactGrid(field_layer,  
                                numPairs, 2, //rows, cols  
                                6, 6,        //initX, initY  
                                6, 6);       //xPad, yPad  
        numPairs++;  
        field_layer.invalidate();  
        this.pack();  
        }  

推荐答案

一个选项是 GridBagConstraints .这是一个教程,可帮助您入门.

One option is GridBagLayout. In order to use this layout properly, you'll need to understand GridBagConstraints. Here's a tutorial to help you get started.

这是一个简单的例子:

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class MyExample 
{
    // Field members
    static JPanel panel = new JPanel();
    static Integer indexer = 1;
    static List<JLabel> listOfLabels = new ArrayList<JLabel>();
    static List<JTextField> listOfTextFields = new ArrayList<JTextField>();

    public static void main(String[] args)
    {       
        // Construct frame
        JFrame frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        frame.setPreferredSize(new Dimension(300, 300));
        frame.setTitle("My Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Frame constraints
        GridBagConstraints frameConstraints = new GridBagConstraints();

        // Construct button
        JButton addButton = new JButton("Add");
        addButton.addActionListener(new ButtonListener());

        // Add button to frame
        frameConstraints.gridx = 0;
        frameConstraints.gridy = 0;
        frame.add(addButton, frameConstraints);

        // Construct panel
        panel.setPreferredSize(new Dimension(200, 200));
        panel.setLayout(new GridBagLayout());
        panel.setBorder(LineBorder.createBlackLineBorder());

        // Add panel to frame
        frameConstraints.gridx = 0;
        frameConstraints.gridy = 1;
        frameConstraints.weighty = 1;
        frame.add(panel, frameConstraints);

        // Pack frame
        frame.pack();

        // Make frame visible
        frame.setVisible(true);
    }

    static class ButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent arg0) 
        {       
            // Clear panel
            panel.removeAll();

            // Create label and text field
            listOfTextFields.add(new JTextField());
            listOfLabels.add(new JLabel("Label " + indexer));

            // Create constraints
            GridBagConstraints textFieldConstraints = new GridBagConstraints();
            GridBagConstraints labelConstraints = new GridBagConstraints();

            // Add labels and text fields
            for(int i = 0; i < indexer; i++)
            {
                // Text field constraints
                textFieldConstraints.gridx = 0;
                textFieldConstraints.gridy = i;

                // Label constraints
                labelConstraints.gridx = 1;
                labelConstraints.gridy = i;

                // Add them to panel
                panel.add(listOfTextFields.get(i), textFieldConstraints);
                panel.add(listOfLabels.get(i), labelConstraints);
            }

            // Align components top-to-bottom
            GridBagConstraints c = new GridBagConstraints();
            c.gridx = 0;
            c.gridy = indexer;
            c.weighty = 1;
            panel.add(new JLabel(), c);

            // Increment indexer
            indexer++;
        }
    }
}

注意:不要将自己局限于此特定的布局管理器.也就是说,您也应该探索其他布局管理器.

Note: don't limit yourself to this particular layout manager. That is, you should explore other Layout Managers too.

这篇关于我想通过单击添加按钮动态添加JLabel和文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆