将 JTextField 添加到 JPanel 并显示它们

发布于 2024-07-14 01:02:57 字数 571 浏览 5 评论 0原文

我正在 NetBeans 中使用 Java 和 Swing 构建一个小应用程序。 使用 NetBeans 设计窗口,我创建了一个内部带有 JPanel 的 JFrame。

现在我想动态添加一些 jTextFields 到 JPanel。 我写了类似的东西:

Vector textFieldsVector = new Vector();
JTextField tf;
int i = 0;
while (i < 3) {
    tf = new JTextField();
    textFieldVector.add(tf);
    myPanel.add(tf); //myPanel is the JPanel where I want to put the JTextFields
    i++;
}
myPanel.validate();
myPanel.repaint();

但没有任何反应:当我运行应用程序时,JFrame 显示其中包含 JPanel,但 JTextFields 没有。

我是编写图形 Java 应用程序的新手,所以我肯定错过了一些非常简单的东西,但我看不出是什么。

I'm building a little app using Java and Swing in NetBeans. Using NetBeans design window, I created a JFrame with a JPanel inside.

Now I want to dynamically add some jTextFields to the JPanel.
I wrote something like that:

Vector textFieldsVector = new Vector();
JTextField tf;
int i = 0;
while (i < 3) {
    tf = new JTextField();
    textFieldVector.add(tf);
    myPanel.add(tf); //myPanel is the JPanel where I want to put the JTextFields
    i++;
}
myPanel.validate();
myPanel.repaint();

But nothing happens: when I run the app, the JFrame shows with the JPanel inside, but the JTextFields don't.

I'm a total newbie in writing graphical Java apps, so I'm surely missing something very simple, but I can't see what.

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

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

发布评论

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

评论(6

土豪 2024-07-21 01:02:57

在 Netbeans GUI 中,将布局管理器设置为 GridLayout 或 FlowLayout 之类的内容(仅用于测试)。 您可以通过转到 GUI 编辑器,单击面板,然后右键单击并选择布局来完成此操作。

更改为不同的布局后,请转到属性并更改布局属性。 对于 GridLayout,您需要确保有 3 个网格单元。

尝试 myPanel.revalidate(),而不是 myPanel.validate()。

更规范的方法是创建一个自定义 JPanel(不使用 GUI 编辑器),该 JPanel 设置自己的布局管理器、用组件填充自身等。然后,在 Netbeans GUI 编辑器中,拖放该自定义 JPanel进入 GUI 编辑器。 Matisse 当然能够处理 Swing 组件的运行时修改,但这不是使用它的正常方法。

In the Netbeans GUI, set the layout manager to something like GridLayout or FlowLayout (just for testing). You can do this by going to the GUI editor, clicking on your panel, and then right-clicking and selecting a layout.

Once you've changed to a different layout, go to the properties and alter the layout properties. For the GridLayout, you want to make sure you have 3 grid cells.

Instead of myPanel.validate(), try myPanel.revalidate().

The more canonical way to do this is to create a custom JPanel (without using the GUI editor) that sets its own layout manager, populates itself with components, etc. Then, in the Netbeans GUI editor, drag-and-drop that custom JPanel into the gui editor. Matisse is certainly capable of handling the runtime-modification of Swing components, but that's not the normal way to use it.

翻身的咸鱼 2024-07-21 01:02:57

自从我完成一些 Swing 以来已经有一段时间了,但我认为您需要回忆 pack() 来告诉框架重新布局其组件

编辑:是的,我知道自从我完成 Swing 以来已经太久了。 我已经敲出了以下代码,它按预期工作并添加了文本字段......

    JFrame frame = new JFrame("My Frame");
    frame.setSize(640, 480);
    JPanel panel = new JPanel();
    panel.add(new JLabel("Hello"));
    frame.add(panel);
    frame.setLayout(new GridLayout());
    frame.pack();
    frame.setVisible(true);
    Vector textFieldVector = new Vector();
    JTextField tf;
    int i = 0;
    while (i < 3) {
        tf = new JTextField();
        textFieldVector.add(tf);
        panel.add(tf); //myPanel is the JPanel where I want to put the JTextFields
        i++;
    }
    panel.validate();
    panel.repaint();

It's been a while since I've done some Swing but I think you'll need to recall pack() to tell the frame to relayout its components

EDIT: Yep, I knew it had been too long since I did Swing. I've knocked up the following code which works as expected though and adds the textfields...

    JFrame frame = new JFrame("My Frame");
    frame.setSize(640, 480);
    JPanel panel = new JPanel();
    panel.add(new JLabel("Hello"));
    frame.add(panel);
    frame.setLayout(new GridLayout());
    frame.pack();
    frame.setVisible(true);
    Vector textFieldVector = new Vector();
    JTextField tf;
    int i = 0;
    while (i < 3) {
        tf = new JTextField();
        textFieldVector.add(tf);
        panel.add(tf); //myPanel is the JPanel where I want to put the JTextFields
        i++;
    }
    panel.validate();
    panel.repaint();
长不大的小祸害 2024-07-21 01:02:57

你的 while 循环是错误的。 i 永远不会增加,因此您的窗口创建处于无限循环中,并且您的 CPU 消耗应该为 100%,直到您中止程序。 此外,当您运行程序时,GUI 应该完全没有响应。

Your while loop is wrong. i never gets incremented so your window creation is in an endless loop and your CPU consumption should be at 100% until you abort the program. Also, the GUI should be completely non-responsive when you run your program.

孤蝉 2024-07-21 01:02:57

使用GroupLayout的通常方法是将组件添加到GroupGroupLayout 保留对其负责的Container 的引用(这是有道理的)。 您不应该在没有限制的情况下将组件添加到面板中。

The usual way to use GroupLayout is to add a component to a Group. GroupLayout keeps a reference to the Container it is responsible for (which makes sense). You shouldn't be adding the component to the panel without constraints.

巷雨优美回忆 2024-07-21 01:02:57

不要将 GroupLayout 与新的(动态添加的)组件一起使用。 它不会显示出来。

Don't use GroupLayout with new (dynamically added) component. It won't show up.

抚你发端 2024-07-21 01:02:57

只需使用 JTextField 的 .setVisible() 方法即可:

JTextField tf = new JTextField() ;
tf.setVisible(true) ;
panel.add(tf) ;

Just use the .setVisible() method of JTextField:

JTextField tf = new JTextField() ;
tf.setVisible(true) ;
panel.add(tf) ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文