gpt4 book ai didi

java - 无法在java中的jscrollPane中将组件添加到jpanel

转载 作者:搜寻专家 更新时间:2023-11-01 03:51:21 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com