我在JProgressBar
gui上使用Swing
:
当我使用progressBar.setValue()
进行更新时最小化窗口时,它会像这样挤压:
请注意,调整大小或类似内容并不能解决问题。
为什么会发生以及如何预防呢?是什么导致的?我希望进度条与第一张图像保持相同的大小。
我正在使用GridBagLayout
。
重现的代码:
import javax.swing.JFrame;
import javax.swing.SwingWorker;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JProgressBar;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
@SuppressWarnings("serial")
public class ProgressBarSqueezeFixedExample extends JFrame
{
public ProgressBarSqueezeFixedExample()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0 };
gridBagLayout.columnWeights = new double[] { 1.0, 0.0, Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
Double.MIN_VALUE };
getContentPane().setLayout(gridBagLayout);
JProgressBar progressBar = new JProgressBar();
JTextArea textArea = new JTextArea();
JButton btnRun = new JButton("Run");
setPreferredSize(new Dimension(200, 200));
btnRun.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
SwingWorker<String, String> worker = new SwingWorker<String, String>()
{
@Override
protected String doInBackground() throws Exception
{
for (int i = 0; i < 10001; i++)
{
try
{
Thread.sleep(1);
} catch (InterruptedException e)
{
e.printStackTrace();
}
progressBar.setValue(i);
progressBar.setString(i + " %");
textArea.append(i + System.lineSeparator());
}
return null;
}
};
worker.execute();
}
});
GridBagConstraints gbc_btnRun = new GridBagConstraints();
gbc_btnRun.insets = new Insets(0, 0, 5, 5);
gbc_btnRun.gridx = 0;
gbc_btnRun.gridy = 0;
getContentPane().add(btnRun, gbc_btnRun);
JLabel lblProgress = new JLabel("Progress");
GridBagConstraints gbc_lblProgress = new GridBagConstraints();
gbc_lblProgress.insets = new Insets(0, 0, 5, 5);
gbc_lblProgress.gridx = 0;
gbc_lblProgress.gridy = 1;
getContentPane().add(lblProgress, gbc_lblProgress);
progressBar.setMaximum(10000);
progressBar.setStringPainted(true);
progressBar.setMinimumSize(progressBar.getPreferredSize()); // Fixes squeezing issues
GridBagConstraints gbc_progressBar = new GridBagConstraints();
gbc_progressBar.insets = new Insets(0, 0, 5, 5);
gbc_progressBar.gridx = 0;
gbc_progressBar.gridy = 2;
getContentPane().add(progressBar, gbc_progressBar);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_progressBar.fill=GridBagConstraints.HORIZONTAL; // Alternate fix
gbc_scrollPane.gridwidth = 2;
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 3;
getContentPane().add(scrollPane, gbc_scrollPane);
scrollPane.setViewportView(textArea);
pack();
setVisible(true);
}
public static void main(String[] args)
{
new ProgressBarSqueezeFixedExample();
}
}
gui的大小调整和压缩进度条必须使用textArea append()看起来如何。
答案 0 :(得分:2)
使用组件显示区域大于组件请求大小时使用的GridBagConstraints#fill属性。它决定是否调整组件大小。
gbc_progressBar.fill=GridBagConstraints.HORIZONTAL;
注意:无需创建GridBagConstraints
的多个实例。你也可以使用单个对象来实现同样的目标。
详细了解How to Use GridBagLayout并查看示例。