让程序自动添加java中的文本字段

发布于 2024-12-28 06:41:42 字数 144 浏览 3 评论 0原文

也许有一个类似的问题,但我找不到。

我希望我的程序( awt 或 swing )自动添加控件(如文本字段)。

例如:一个对话框程序有 10 个用于输入名称的字段,但我需要 11 个,因此按下按钮就会出现一个新字段。

先感谢您。

Maybe there is a similar question to this, but I couldn't find a one.

Id like my program ( awt or swing ) to add controls ( like text fields ) automatically.

For example : A dialog program has 10 fields for inputting names, but I need 11 so by pressing a button a new field will appear.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给妤﹃绝世温柔 2025-01-04 06:41:42

这是使用 Box 的示例:

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.event.*;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class MultiJComponentsTest extends JFrame
{
    private JButton btnAdd;
    private JPanel centerPanel;
    private Box vBox;

    public MultiJComponentsTest()
    {
        super("The Title");
        btnAdd = new JButton("Add new JTextField!");
        btnAdd.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e)
            {
                vBox.add(new JTextField(20));
                pack();
            }

        });
        vBox = Box.createVerticalBox();
        centerPanel = new JPanel();
        JPanel contentPanel = (JPanel) getContentPane();
        contentPanel.setLayout(new BorderLayout());
        contentPanel.add(btnAdd, "South");
        contentPanel.add(centerPanel, "Center");
        centerPanel.add(vBox);
        pack();
    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new MultiJComponentsTest().setVisible(true);
            }
        });
    }
}

Here is an example using Box:

enter image description here

import java.awt.BorderLayout;
import java.awt.event.*;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class MultiJComponentsTest extends JFrame
{
    private JButton btnAdd;
    private JPanel centerPanel;
    private Box vBox;

    public MultiJComponentsTest()
    {
        super("The Title");
        btnAdd = new JButton("Add new JTextField!");
        btnAdd.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e)
            {
                vBox.add(new JTextField(20));
                pack();
            }

        });
        vBox = Box.createVerticalBox();
        centerPanel = new JPanel();
        JPanel contentPanel = (JPanel) getContentPane();
        contentPanel.setLayout(new BorderLayout());
        contentPanel.add(btnAdd, "South");
        contentPanel.add(centerPanel, "Center");
        centerPanel.add(vBox);
        pack();
    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new MultiJComponentsTest().setVisible(true);
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文