Java 将 JTextField 添加到 JPanel 并显示它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/546144/
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-08-11 16:04:15  来源:igfitidea点击:

Adding JTextField to a JPanel and showing them

javaswingjpaneljtextfield

提问by Davide Gualano

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

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

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

现在我想动态地向 JPanel 添加一些 jTextFields。我写了这样的东西:

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.

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

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

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

采纳答案by James Schek

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.

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

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.

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

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

而不是 myPanel.validate(),试试 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.

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

回答by Bombe

Your while loop is wrong. inever 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.

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

回答by tddmonkey

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

自从我做了一些 Swing 已经有一段时间了,但我认为你需要回忆一下 pack() 来告诉框架重新布局它的组件

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...

编辑:是的,我知道自从我做 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();

回答by Tom Hawtin - tackline

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

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

回答by Cork

Just use the .setVisible()method of JTextField:

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

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

回答by Raymond Gao

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

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