简体   繁体   English

摆动:动态更改组件的可见性

[英]Swing: Dynamically change visibility of components

When I set the visibility of components (mainly JPanels that have other child components) to false or back to true later, do I have to call .revalidate() and .repaint() on the parent container explicitly or do the LayoutManagers handle this automatically? 当我将组件(主要是具有其他子组件的JPanel)的可见性设置为false或稍后恢复为true时,我是否必须在父容器上显式调用.revalidate().repaint()或LayoutManagers自动处理此问题?

When I tried this with a component in BorderLayout-North position for example, everything looked fine without calling revalidate. 例如,当我尝试使用BorderLayout-North位置的组件进行此操作时,一切看起来不错,而无需调用revalidate。 Just not sure if it will work on all platforms and with all LayoutManagers. 只是不确定它是否可以在所有平台上和所有LayoutManagers上运行。

when u set the visibility of components like JPanels. 当您设置诸如JPanels之类的组件的可见性时。 JVM automatically call revalidate() and repaint() method when u set the visibility to true. 当您将可见性设置为true时,JVM会自动调用revalidate()和repaint()方法。 u does not need to call the all these method. 您不需要调用所有这些方法。 and it will work for all layout 它将适用于所有布局

The following code may solve your problem 以下代码可以解决您的问题

      p2_wrkrreg=new JPanel();
      p2_wrkrreg.setBounds(201,0,830,720);
     // p2_wrkrreg.setLayout(null);
      //p2_wrkrreg.setBackground(Color.white);
      p2_wrkrreg.setVisible(false);

In this code, 在这段代码中

The JPanel declared and make it invisible, it will be visible on a button click JPanel声明并使其不可见,单击按钮即可看到

@Override
public void actionPerformed(ActionEvent e) {       

if(e.getSource()==btn_wrkrreg)
    {
        p2_wrkrreg.setVisible(true);
    }

here btn_wrkrreg is a JButton 这里btn_wrkrreg是一个JButton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM