java:单击按钮动态生成的文本框

时间:2012-05-17 08:45:31

标签: java swing

点击“添加”按钮后,我创建了文本字段和标签。我给出了x和y坐标,但文本框出现的方式不正确。 怎么纠正呢?以及如何增加文本框的宽度???

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 SS 
{
// 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(800, 800));
    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(400, 400));
    panel.setLayout(new GridBagLayout());
    panel.setBorder(LineBorder.createBlackLineBorder());

    // Add panel to frame
    frameConstraints.gridx = 0;
    frameConstraints.gridy = 1;
    frameConstraints.weighty = 20;
    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("Name " + 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 = 20;
            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++;
    }
}

}

2 个答案:

答案 0 :(得分:3)

要使用文本框刷新帧,您需要在actionPerformed方法的底部调用pack。

frame.pack();

为此,您需要将框架作为类变量。

static JFrame frame;

对于尺寸,网格袋布局将覆盖您的setSize,因此您可以为其赋予重量并使其拉伸以填充空间。这可以在你的其他textFieldContraints调用之后。

textFieldConstraints.weightx = 1;
textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;

当你按下按钮并拿起盒子时,这应该会出现文本框。

答案 1 :(得分:0)

增加文本字段的宽度。

listOfTextFields.add(new JTextField(null,10));