我正在使用 Java 开发桌面应用程序。我放置了一个滚动 Pane 并在其中放置了一个面板,以添加一个名为 name
的标签和一个空的 TextField 以及一个名为 more
的按钮。我想要的是单击按钮时,第一个标签和文本字段下会出现一个新的标签和一个文本字段,但什么也没有出现。这是我的按钮代码:
private void moreActionPerformed(java.awt.event.ActionEvent evt) {
JLabel nameLabel = new JLabel("Name :");
JPanel panel = new JPanel();
i++;
nameLabel.setBounds(
child_namelabel.getX(), child_namelabel.getY() + 35 * i,
child_namelabel.getWidth(), child_namelabel.getHeight());
panel.add(nameLabel);
children.add(panel);
}
最佳答案
来自 Java-Api:
This method changes layout-related information, and therefore, invalidates the component hierarchy.
If the container has already been displayed, the hierarchy must be validated thereafter in order to
display the added component.
所以你调用:
children.validate();
在你的方法结束时。
关于java - 无法在java中的jscrollPane中将组件添加到jpanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27127715/