[英]How can i reset a JProgressBar in java?
I have a button and when i click it, then it will start the progressbar
. 我有一个按钮,当我单击它时,它将启动
progressbar
栏。 After finishing one time, i want the progressbar to reset and if i click on the button again then it will start fresh. 完成一次后,我希望重置进度条,如果再次单击该按钮,它将重新开始。
Here's my code: 这是我的代码:
else if (action == detectButton) {
// pb.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
pb.setValue(0);
pb.repaint();
}
});
for (int i = 1; i <= 10; i++) {
final int val = i;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
pb.setValue(val);
pb.repaint();
try {
java.lang.Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
Now the problem is that when i first run it runs ok but in second attempt it is not resetting the progressbar to 0. How to do it actually?? 现在的问题是,当我第一次运行它时,它运行正常,但是在第二次尝试中,它没有将进度条重置为0。 实际上该怎么做?
EDIT: I am repainting the progressbar everytime but i saw that it is not smooth enough to show that 10% happened then 20%.. 编辑: 我每次都重新绘制进度条,但我看到它还不够平滑,无法显示10%然后发生了20%。
Your JProgressbar doesn't reset, because it will be repaint after the first Runnable finished. 您的JProgressbar不会重置,因为它将在第一个Runnable完成后重新绘制。 But your
java.lang.Thread.sleep(2000);
但是你的
java.lang.Thread.sleep(2000);
prevent this. 防止这种情况。
Here is a full example, how to use the progressbar: 这是一个完整的示例,说明如何使用进度条:
public class TestWindow
{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
TestWindow window = new TestWindow();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestWindow()
{
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize()
{
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
final JProgressBar progressBar = new JProgressBar();
progressBar.setBounds(10, 11, 414, 14);
frame.getContentPane().add(progressBar);
JButton btnNewButton = new JButton("Reset");
btnNewButton.setBounds(10, 228, 89, 23);
frame.getContentPane().add(btnNewButton);
JButton button = new JButton("Run");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Runnable update = new Runnable()
{
@Override
public void run()
{
for (int i = 1; i <= 100; i++)
{
final int val = i;
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
progressBar.setValue(val);
progressBar.repaint();
}
});
try
{
java.lang.Thread.sleep(100);
}
catch (InterruptedException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
};
Thread t = new Thread(update);
t.start();
}
});
button.setBounds(109, 228, 89, 23);
frame.getContentPane().add(button);
btnNewButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
progressBar.setValue(0);
progressBar.repaint();
}
});
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.