将 JTextField 添加到 JPanel 并显示它们
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 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.
自从我完成一些 Swing 以来已经有一段时间了,但我认为您需要回忆 pack() 来告诉框架重新布局其组件
编辑:是的,我知道自从我完成 Swing 以来已经太久了。 我已经敲出了以下代码,它按预期工作并添加了文本字段......
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...
你的 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.使用
GroupLayout
的通常方法是将组件添加到Group
。GroupLayout
保留对其负责的Container
的引用(这是有道理的)。 您不应该在没有限制的情况下将组件添加到面板中。The usual way to use
GroupLayout
is to add a component to aGroup
.GroupLayout
keeps a reference to theContainer
it is responsible for (which makes sense). You shouldn't be adding the component to the panel without constraints.不要将 GroupLayout 与新的(动态添加的)组件一起使用。 它不会显示出来。
Don't use GroupLayout with new (dynamically added) component. It won't show up.
只需使用 JTextField 的
.setVisible()
方法即可:Just use the
.setVisible()
method of JTextField: