我在java中创建了一个ProgressBar。我可以使用pb.setValue(i)设置值;我是一个int 我无法设置'i'的值来增加进度条的值,以便在10秒内进度条在10秒内达到1%到100%。我怎样才能做到这一点 ?任何帮助将不胜感激
答案 0 :(得分:1)
您可以使用Timer:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TimerTest {
public static void main(String[] args) {
final JFrame frame = new JFrame();
final JProgressBar ps = new JProgressBar();
final Timer timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int value = ps.getValue() + 1;
ps.setValue(value);
}
});
ps.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (ps.getValue() == 100) {
timer.stop();
frame.dispose();
}
}
});
frame.setSize(600, 400);
frame.add(ps);
frame.pack();
frame.setVisible(true);
timer.start();
}
}
答案 1 :(得分:0)
查看java标准库,有时间特定的功能,例如此代码片段。
long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();
long duration = endTime - startTime;
您可以根据实施代码的方式设置结束时间和开始时间,以便产生持续时间或10或1个部分。
答案 2 :(得分:0)
您有多个选择,这里有两个。
您可以建立一个计时器,每秒计时一次,直到它勾选10次,或者您可以计算已经过的时间量并根据超过目标时间的时间生成进度值,这将产生更平滑的进展...
public class ProgressOverTime {
public static void main(String[] args) {
new ProgressOverTime();
}
public ProgressOverTime() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(new TestPane(), gbc);
frame.add(new TimeGapPane(), gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JProgressBar pb;
private int progress;
private int target = 10;
public TestPane() {
setLayout(new GridBagLayout());
pb = new JProgressBar();
pb.setMaximum(10);
add(pb);
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
progress++;
if (progress > target) {
((Timer)e.getSource()).stop();
}
pb.setValue(progress);
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
}
public class TimeGapPane extends JPanel {
private JProgressBar pb;
private long startTime = -1;
private int target = 10000;
public TimeGapPane() {
setLayout(new GridBagLayout());
pb = new JProgressBar();
pb.setMaximum(100);
add(pb);
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (startTime < 0) {
startTime = System.currentTimeMillis();
}
long now = System.currentTimeMillis();
long ticks = now - startTime;
float progress = (float)ticks / (float)target;
if (progress >= 1f) {
((Timer)e.getSource()).stop();
progress = 1f;
}
pb.setValue(Math.round(100f * progress));
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
}
}