我不明白为什么进度条不会在以下代码中移动:
JDialog dlgProgress = new JDialog(InterfaceYadis.frameInterface, "Veuillez patienter...", true);
JLabel lblStatus = new JLabel("Test");
JProgressBar pbProgress = new JProgressBar(0, 100);
pbProgress.setIndeterminate(true);
dlgProgress.add(BorderLayout.NORTH, lblStatus);
dlgProgress.add(BorderLayout.CENTER, pbProgress);
dlgProgress.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//dlgProgress.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dlgProgress.setSize(300, 90);
dlgProgress.setLocationRelativeTo(null);
SwingWorker<Void, Void> sw = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
Thread.sleep(2500);
return null;
}
@Override
protected void done() {
dlgProgress.dispose();
}
};
sw.execute();
dlgProgress.setVisible(true);
JDialog应该在UI线程上执行,进度条应该从左向右移动? 感谢。
修改
问题不在于代码,之前,我创建了一个自定义的JProgressBar,并且我修改了重绘间隔和循环时间默认值... 自定义Jprogress正在设置:
UIManager.put("ProgressBar.repaintInterval", new Integer(50));
UIManager.put("ProgressBar.cycleTime", new Integer(100));
默认设置为:
INTERVAL = UIManager.getInt("ProgressBar.repaintInterval"); // = 50
CYCLETIME = UIManager.getInt("ProgressBar.cycleTime"); // = 3000
对我来说,我有两个解决方案:
1 /我每次定义周期和间隔以确保具有正确的值
2 /我在任何地方使用相同的进度条
我认为解决方案2是最好的,但我想知道解决方案是否也是一个很好的解决方案。
答案 0 :(得分:1)
如果你希望从左到右稳定运动:
例如两者:
import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class ProgressBarEg extends JPanel {
public ProgressBarEg() {
add(new JButton(new ShowDialogAction("Show Indeterminant Dialog", KeyEvent.VK_I, true)));
add(new JButton(new ShowDialogAction("Show Determinant Dialog", KeyEvent.VK_D, false)));
setPreferredSize(new Dimension(500, 350));
}
private class MyWorker extends SwingWorker<Void, Void> {
private static final int DELTA_PROGRESS = 1;
private static final long SLEEP_TIME = 40;
@Override
protected Void doInBackground() throws Exception {
int progress = getProgress();
while (progress < 100) {
progress += DELTA_PROGRESS;
progress = Math.min(progress, 100);
setProgress(progress);
Thread.sleep(SLEEP_TIME);
}
return null;
}
}
private class ShowDialogAction extends AbstractAction {
JDialog dlgProgress;
private boolean indeterminant;
JProgressBar pbProgress = new JProgressBar(0, 100);
public ShowDialogAction(String name, int mnemonic, boolean indeterminant) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
this.indeterminant = indeterminant;
}
@Override
public void actionPerformed(ActionEvent e) {
if (dlgProgress == null) {
Window window = SwingUtilities.getWindowAncestor(ProgressBarEg.this);
dlgProgress = new JDialog(window, "Veuillez patienter...", ModalityType.APPLICATION_MODAL);
JLabel lblStatus = new JLabel("Test");
if (indeterminant) {
pbProgress.setIndeterminate(true);
} else {
pbProgress.setStringPainted(true);
}
dlgProgress.add(BorderLayout.NORTH, lblStatus);
dlgProgress.add(BorderLayout.CENTER, pbProgress);
dlgProgress.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//dlgProgress.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dlgProgress.setPreferredSize(new Dimension(300, 90));
dlgProgress.setLocationRelativeTo(null);
dlgProgress.pack();
}
SwingWorker<Void, Void> sw = new MyWorker();
sw.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equalsIgnoreCase("progress")) {
pbProgress.setValue((Integer) evt.getNewValue());
} else if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
dlgProgress.dispose();
}
}
});
sw.execute();
dlgProgress.setVisible(true);
}
}
private static void createAndShowGui() {
ProgressBarEg mainPanel = new ProgressBarEg();
JFrame frame = new JFrame("ProgressBarEg");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}