gpt4 book ai didi

java - JProgressBar 不可见时不占用空间

转载 作者:搜寻专家 更新时间:2023-11-01 02:05:41 25 4
gpt4 key购买 nike

我希望能够隐藏 JProgressBar 并让它所在的面板不改变大小。

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

Progress Bar Visible

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

Progress Bar Not Visible

  • 显然强制 JPanel 为特定大小没有帮助。
  • 在 JProgressBar 上设置明确的大小似乎没有帮助。
  • 利用 NetBean 的 GUI 编辑器中的垂直间隙没有似乎有帮助(我将其设为固定大小)。

我想我在这里遗漏了一些简单的东西,我看不出它是什么。

我如何强制隐藏的 JProgressBar 仍然占据窗体中的不动产(尤其是垂直不动产)?

代码如下,物有所值。与代码的细节相比,我对我错过的概念更感兴趣。 :-)

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

最佳答案

选项:将其放置在 JPanel 中,该 JPanel 使用 CardLayout 将 View 换入和换出。你可以用一个空的 JLabel 来交换它。 CardLayout - 使用 JPanel 将以显示所有交换组件所需的最大尺寸调整大小。然后在要显示 JProgressBar 的位置添加 CardLayout-using JPanel。

以您的代码为例(尽管我讨厌 GroupLayout):

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;
}

如果这是我的项目,我会考虑为此创建一个类,比如称为 SwappingComponent 的东西,它可能看起来像这样:

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();
}
});
}

}

关于java - JProgressBar 不可见时不占用空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34518725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com