【问题标题】:Can't add component to panel in scrollPane in java无法在java中的scrollPane中将组件添加到面板
【发布时间】:2015-01-23 12:22:34
【问题描述】:

我正在使用 Java 开发桌面应用程序。我放置了一个滚动窗格并在其中放置了一个面板,以添加一个名为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 user-interface panel jscrollpane


    【解决方案1】:

    来自 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();
    

    在你的方法结束时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      相关资源
      最近更新 更多