有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

JFrame

  • 带组件的面板
  • _按钮3
  • 面板与面板
  • _PanelOne
  • __按钮1
  • __标签1(背景)
  • _第二小组
  • __标签2(背景)
  • _u按钮2

    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) 个答案

  1. # 1 楼答案

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

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

    来自Container的add方法

    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.

    从^{的remove方法

    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的评论进行编辑