0

我在单击“添加”按钮时创建了文本字段和标签。我已经给出了 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++;
    }
}

}

4

2 回答 2

3

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

frame.pack();

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

static JFrame frame;

对于尺寸,网格袋布局将覆盖您的 setSize,因此您可以给它一个权重并使其伸展以填充空间。这可以在您的其他 textFieldContraints 调用之后进行。

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

当您按下按钮并拿起框时,这应该会使文本框出现。

于 2012-05-17T09:43:40.530 回答
0

增加文本字段的宽度。

listOfTextFields.add(new JTextField(null,10));
于 2013-04-14T18:43:04.337 回答