我正在 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 应用程序的新手,所以我肯定遗漏了一些非常简单的东西,但我看不出是什么。