组件不可见然后在Java中的窗格之间切换

时间:2013-12-29 10:59:20

标签: java components hidden panels

问题是我在窗格之间切换,我的按钮和文本字段变得不可见,但是然后我将光标拖到组件上,它们就出现了。 我的层次结构与此类似:

的JFrame

  • PanelWithComponents
  • _Button3
  • PanelWithPanels
  • _PanelOne
  • __ Button1的
  • __ Label1的(背景)
  • _PanelTwo
  • __ Label2的(背景)
  • __ Button2的

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
     if (jRadioButton1.isSelected()){
        CardLayout cl = (CardLayout)(bottomPanel.getLayout());
        cl.next(bottomPanel);
    
    }else{
        CardLayout cl = (CardLayout)(bottomPanel.getLayout());
        cl.next(bottomPanel);
    
    
     }
    

    }

1 个答案:

答案 0 :(得分:0)

切换窗格时,应调用revalidate()repaint()方法。

frame.getContentPane().revalidate();
frame.getContentPane().repaint();

来自Container的添加方法。

Note: If a component has been added to a container that has been displayed, validate must be called on that container to display the new component. If multiple components are being added, you can improve efficiency by calling validate only once, after all the components have been added.

Container的删除方法。

Note: If a component has been removed from a container that had been displayed, validate() must be called on that container to reflect changes. If multiple components are being removed, you can improve efficiency by calling validate() only once, after all the components have been removed.

按照camickr的评论编辑。