Invisible components still take up space JPanel

前端 未结 6 1308
渐次进展
渐次进展 2021-01-18 05:22

I have a series of components underneath each other in a JPanel set as a GridLayout. I need to temporarily hide the components but setVisible(false) doesn\'t cu

6条回答
  •  情话喂你
    2021-01-18 06:12

    try to use BoxLayout with component alignment. For example:

    JPanel innerPane = new JPanel();
    BoxLayout innerPaneLayout = new BoxLayout(innerPane,BoxLayout.Y_AXIS);
    innerPane.setLayout(innerPaneLayout);
    
    for (int i = 0; i < 30; i++)
    {
         JPane newPane = getPane();
         innerPane.add(newPane);
         newPane.setAlignmentY(Component.TOP_ALIGNMENT);
    }
    
    for (int i = 0; i < 30; i++)
    {
          if (i%2==0)
             innerPane.getComponent(i).setVisible(false);
    }
    

提交回复
热议问题