Java JProgressBar不可见时不占用空间

Java JProgressBar不可见时不占用空间,java,swing,jprogressbar,Java,Swing,Jprogressbar,我希望能够隐藏JProgressBar,并使其所在的面板不改变大小 目前,当我将进度条设置为可见时(通过pb.setVisible(true)),我得到如下布局: 然后,当我将其设置为“不可见”时,我得到以下布局: 显然,强迫JPanel成为特定的大小并没有帮助 在JProgressBar上设置显式大小似乎没有帮助 在NetBean的GUI编辑器中操纵垂直间隙并没有起到任何作用 看起来很有帮助(我把它做成了固定尺寸) 我想我错过了一些简单的东西,我看不出是什么 如何强制隐藏的JProgr

我希望能够隐藏JProgressBar,并使其所在的面板不改变大小

目前,当我将进度条设置为可见时(通过
pb.setVisible(true)
),我得到如下布局:

然后,当我将其设置为“不可见”时,我得到以下布局:

  • 显然,强迫JPanel成为特定的大小并没有帮助
  • 在JProgressBar上设置显式大小似乎没有帮助
  • 在NetBean的GUI编辑器中操纵垂直间隙并没有起到任何作用 看起来很有帮助(我把它做成了固定尺寸)
我想我错过了一些简单的东西,我看不出是什么

如何强制隐藏的JProgressBar仍以表单形式占用不动产(尤其是垂直不动产)

下面是代码,说明它的价值。我对我错过的概念比对代码的细节更感兴趣。:-)

包装测试;
公共类MyFrame扩展了javax.swing.JFrame{
公共MyFrame(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
jPanel1=newjavax.swing.JPanel();
pb=newjavax.swing.JProgressBar();
bnVis=newjavax.swing.JButton();
bnInvi=newjavax.swing.JButton();
jButton1=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBorder(javax.swing.BorderFactory.create蚀刻边界());
bnVis.setText(“可见”);
addActionListener(新的java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的BNT(evt);
}
});
bnInvis.setText(“不可见”);
bnInvi.addActionListener(新的java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的BNT(evt);
}
});
jButton1.setText(“其他一些随机组件”);
javax.swing.GroupLayout jPanel1Layout=新的javax.swing.GroupLayout(jPanel1);
setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jpanellayout.createSequentialGroup()
.addGap(19,19,19)
.addComponent(pb,javax.swing.GroupLayout.PREFERRED_SIZE,217,javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(45,简称最大值))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u值)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,jPanel1Layout.createSequentialGroup()
.addComponent(bnVis)
.addGap(28,28,28)
.addComponent(bnInvis)
.addGap(47,47,47))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(30,30,30)))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,jPanel1Layout.createSequentialGroup()
.addContainerGap(20,简称最大值)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bnInvis)
.addComponent(bnVis))
.addGap(4,4,4)
.addComponent(pb,javax.swing.GroupLayout.PREFERRED\u大小,35,javax.swing.GroupLayout.PREFERRED\u大小)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1))
);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addContainerGap(22,简称最大值)
.addComponent(jPanel1,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addContainerGap(64,简称最大值))
);
包装();
}//                         
私有void bnVisActionPerformed(java.awt.event.ActionEvent evt){
pb.setVisible(真);
}                                     
私有void bnInvisionPerformed(java.awt.event.ActionEvent evt){
pb.setVisible(假);
}                                       
公共静态void main(字符串参数[]){
invokeLater(new Runnable()){
公开募捐{
new MyFrame().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JButton bnInvis;
私有javax.swing.JButton bnVis;
私有javax.swing.JButton jButton1;
私有javax.swing.JPanel jPanel1;
私有javax.swing.JProgressB
package pbtest;

public class MyFrame extends javax.swing.JFrame {

public MyFrame() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    pb = new javax.swing.JProgressBar();
    bnVis = new javax.swing.JButton();
    bnInvis = new javax.swing.JButton();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

    bnVis.setText("Visible");
    bnVis.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            bnVisActionPerformed(evt);
        }
    });

    bnInvis.setText("Not Visible");
    bnInvis.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            bnInvisActionPerformed(evt);
        }
    });

    jButton1.setText("Some Other Random Component");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(19, 19, 19)
            .addComponent(pb, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(45, Short.MAX_VALUE))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addComponent(bnVis)
                    .addGap(28, 28, 28)
                    .addComponent(bnInvis)
                    .addGap(47, 47, 47))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addComponent(jButton1)
                    .addGap(30, 30, 30))))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(20, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(bnInvis)
                .addComponent(bnVis))
            .addGap(4, 4, 4)
            .addComponent(pb, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jButton1))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(22, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(64, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

private void bnVisActionPerformed(java.awt.event.ActionEvent evt) {                                      
   pb.setVisible(true);
}                                     

private void bnInvisActionPerformed(java.awt.event.ActionEvent evt) {                                        
    pb.setVisible(false);
}                                       

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MyFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton bnInvis;
private javax.swing.JButton bnVis;
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JProgressBar pb;
// End of variables declaration                   
import java.awt.CardLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class MyFrame extends javax.swing.JFrame {

    private static final String PROGRESS_BAR = "progress bar";
    private static final String EMPTY = "empty";
    public MyFrame() {
        initComponents();
    }

    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        pb = new javax.swing.JProgressBar();
        bnVis = new javax.swing.JButton();
        bnInvis = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        //!!
        cardLayout = new CardLayout();
        cardHolderPanel = new JPanel(cardLayout);
        cardHolderPanel.add(pb, PROGRESS_BAR);
        cardHolderPanel.add(new JLabel(), EMPTY);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        bnVis.setText("Visible");
        bnVis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnVisActionPerformed(evt);
            }
        });

        bnInvis.setText("Not Visible");
        bnInvis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnInvisActionPerformed(evt);
            }
        });

        jButton1.setText("Some Other Random Component");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        jPanel1Layout
                                .createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addComponent(cardHolderPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 217,
                                        javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(45, Short.MAX_VALUE))
                .addGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING,
                        jPanel1Layout
                                .createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(
                                        jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(
                                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                                        jPanel1Layout.createSequentialGroup().addComponent(bnVis)
                                                                .addGap(28, 28, 28).addComponent(bnInvis)
                                                                .addGap(47, 47, 47))
                                                .addGroup(
                                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                                        jPanel1Layout.createSequentialGroup().addComponent(jButton1)
                                                                .addGap(30, 30, 30)))));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING,
                        jPanel1Layout
                                .createSequentialGroup()
                                .addContainerGap(20, Short.MAX_VALUE)
                                .addGroup(
                                        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(bnInvis).addComponent(bnVis))
                                .addGap(4, 4, 4)
                                .addComponent(cardHolderPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 35,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton1)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup()
                        .addContainerGap(22, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(64, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>

    private void bnVisActionPerformed(java.awt.event.ActionEvent evt) {
        // pb.setVisible(true);
        cardLayout.show(cardHolderPanel, PROGRESS_BAR);
    }

    private void bnInvisActionPerformed(java.awt.event.ActionEvent evt) {
        // pb.setVisible(false);  // !!
        cardLayout.show(cardHolderPanel, EMPTY);;
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MyFrame().setVisible(true);
            }
        });
    }

    private javax.swing.JButton bnInvis;
    private javax.swing.JButton bnVis;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private CardLayout cardLayout;
    private JPanel cardHolderPanel;
    private javax.swing.JProgressBar pb;
}
import java.awt.CardLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

public @SuppressWarnings("serial")
class SwappingComponent extends JPanel {
    private static final String SHOW = "show";
    private static final String HIDE = "hide";
    JComponent component;
    CardLayout cardLayout = new CardLayout();

    public SwappingComponent(JComponent component) {
        setLayout(cardLayout);
        this.component = component;
        add(component, SHOW);
        add(new JLabel(), HIDE);
    }

    public JComponent getComponent() {
        return component;
    }

    public void showComponent(boolean show) {
        String key = show ? SHOW : HIDE;
        cardLayout.show(this, key);
    }

}
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.Border;

@SuppressWarnings("serial")
public class MyGui2 extends JPanel {
    private JProgressBar progressBar = new JProgressBar(0, 100);
    private SwappingComponent swappingComponent = new SwappingComponent(progressBar);

    public MyGui2() {
        progressBar.setStringPainted(true);
        int delay = 100;
        new Timer(delay, new ActionListener() {
            private int value = 0;

            @Override
            public void actionPerformed(ActionEvent e) {
                progressBar.setValue(value);

                if (value >= 100) {
                    value = 0;
                } else {
                    value += 2;
                    value = Math.min(value, 100);
                }
            }
        }).start();
        JPanel northPanel = new JPanel(new GridLayout(1, 0, 5, 5));
        northPanel.add(new JButton(new ShowAction("Show")));
        northPanel.add(new JButton(new HideAction("Hide")));

        JPanel southPanel = new JPanel();
        southPanel.add(new JButton("Some Component"));

        Border outsideBorder = BorderFactory.createEmptyBorder(15, 15, 15, 15);
        Border insideBorder = BorderFactory.createEtchedBorder();
        Border border = BorderFactory.createCompoundBorder(outsideBorder, insideBorder);
        setBorder(border);
        setLayout(new BorderLayout(6, 6));
        add(swappingComponent, BorderLayout.CENTER);
        add(northPanel, BorderLayout.PAGE_START);
        add(southPanel, BorderLayout.PAGE_END);
    }

    private class ShowAction extends AbstractAction {
        public ShowAction(String name) {
            super(name);
            putValue(MNEMONIC_KEY, (int) name.charAt(0));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            swappingComponent.showComponent(true);
        }
    }

    private class HideAction extends AbstractAction {
        public HideAction(String name) {
            super(name);
            putValue(MNEMONIC_KEY, (int) name.charAt(0));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            swappingComponent.showComponent(false);
        }
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("MyGui2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MyGui2());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }

}