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

标签 java user-interface panel jscrollpane

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

相关文章:

css - 如何在 Bootstrap 面板标题中设置内联复选框

c# - C# 中的动画面板

java - 如何使用 GSON/JSON 将字符串数组转换为对象?

java - fatal error 和非致命运行时错误有什么区别?

objective-c - 工作表从 View 中弹出

MATLAB:自动调整 GUI 组件/字体的大小

java - 页面首次加载时 GWT map 标记标注显示为空白

java - 添加 String Literals 和 String 对象之间的区别

c++ - GTK TreeView /存储列扩展

jquery-mobile - 更改 JQM 面板的宽度