Java- 如何通过单击按钮添加更多文本字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12251618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:10:46  来源:igfitidea点击:

Java- How to add more textfields by clicking a button?

javaswingjbuttonjtextfield

提问by SunnY

I have created a framein Java which has some textfields and buttonsin it. Assuming that user wants more textfields (for example to add more data), I want to put a buttonand when a user clicks the button, then a new textfieldshould appear. then user can fill data in it and again by clicking that buttonanother textfieldshould appear. How can I do this ? What code I need to write for the button to show more and more text fields by clicking button? Thank you !

frame在 Java 中创建了一个,其中有一些textfields and buttons。假设用户想要更多的文本字段(例如添加更多数据),我想放一个button,当用户点击时the buttontextfield应该会出现一个新的。然后用户可以在其中填充数据,然后再次点击button另一个textfield应该出现的数据。我怎样才能做到这一点 ?我需要为按钮编写什么代码才能通过单击按钮显示越来越多的文本字段?谢谢 !

回答by nIcE cOw

It would be wise that instead of adding components to your JFramedirectly, you add them to a JPanel. Though related to your problem, have a look at this small example, hopefully might be able to give you some hint, else ask me what is out of bounds.

明智的做法是不要JFrame直接将组件添加到您的文件中,而是将它们添加到JPanel. 虽然与您的问题有关,但请看一下这个小例子,希望能给您一些提示,否则问我什么是越界。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JFrameExample
{   
    private JFrame frame;
    private JButton button;
    private JTextField tfield;
    private String nameTField;
    private int count;

    public JFrameExample()
    {
        nameTField = "tField";
        count = 0;
    }

    private void displayGUI()
    {
        frame = new JFrame("JFrame Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(0, 1, 2, 2));
        button = new JButton("Add JTextField");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                tfield = new JTextField();
                tfield.setName(nameTField + count);
                count++;
                frame.add(tfield);
                frame.revalidate();  // For JDK 1.7 or above.
                //frame.getContentPane().revalidate(); // For JDK 1.6 or below.
                frame.repaint();
            }
        });
        frame.add(button);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new JFrameExample().displayGUI();
            }
        });
    }
}

回答by Dan D.

Supposing that you have a main container called paneland a button variable buttonwhich is already added to panel, you can do:

假设您有一个调用的主容器panel和一个button已添加到的按钮变量panel,您可以执行以下操作:

// handle the button action event
button.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    // create the new text field
    JTextField newTextField = new JTextField();
    // add it to the container
    panel.add(newTextField);
    panel.validate();
    panel.repaint();
  }
});

When adding the new text field, you may need to mention some layout related characteristics, depending on the layout manager you are using (for instance if you use GridBagLayout, you will need to specify the constraints).

添加新文本字段时,您可能需要提及一些与布局相关的特征,具体取决于您使用的布局管理器(例如,如果您使用GridBagLayout,则需要指定约束)。