[英]Why doesn't the progress bar progress?
Following function is called to move the progress bar but I don't know why it doesn't move till other process just works on. 调用以下函数来移动进度条,但我不知道为什么直到其他进程都正常工作后它才会移动。
private void startProgressBar() {
signInProgressBar.setMinimum(0);
signInProgressBar.setMaximum(10);
Runnable r = new Runnable() {
@Override
public void run() {
int p = 1;
while(!loginCompleted) {
signInProgressBar.setValue(p);
//System.out.println(p);
p++;
try {Thread.sleep(5000);}catch(Exception exc) {}
}
}
};
new Thread(r,"progress_bar_thread").start();
}
Snippet that calls startProgressBar
: 调用
startProgressBar
代码startProgressBar
:
startProgressBar(); // CALL
String username = usernameTextField.getText();
String password = new String(passwordField.getPassword());
Openfire server = new Openfire();
boolean isConnected = server.connect(username,password);
if(isConnected) {
// Stash the username and password
User user = new User();
user.setUsername(username);
user.setPassword(password);
// Stop the progress bar
loginCompleted = true;
// Display the next window
UserGUI blab = new UserGUI();
blab.setVisible(true);
this.dispose(); // Dispose off the login window
}
What could be the problem ? 可能是什么问题呢 ?
Your code doesn't appear to adjust the progress bar at all: 您的代码似乎根本无法调整进度条:
signInProgressBar.setValue(5); // <-- in your while loop
This just continually sets the bar's position to 5
, which appears to be about 50% on your scale. 这只会连续将条形图的位置设置为
5
,这似乎是您的比例的50%左右。 Perhaps if you are unsure of the length of your task, you should make your progress bar indeterminate ? 也许如果不确定任务的长度,应该使进度条不确定吗?
You should also be using the EDT to adjust the progress bar with setValue()
. 您还应该使用EDT通过
setValue()
调整进度条。 See the Concurrency in Swing tutorial for more details. 有关更多详细信息,请参见Swing中的并发教程 。
All of this stuff: 所有这些东西:
Openfire server = new Openfire();
boolean isConnected = server.connect(username,password);
if(isConnected) {
// Stash the username and password
User user = new User();
user.setUsername(username);
user.setPassword(password);
// Stop the progress bar
loginCompleted = true;
// ....
... should be done on a background thread, especially connecting and interacting with the server, else you'll be tying up the Swing event thread and with it your entire GUI. ...应该在后台线程上完成,尤其是在与服务器连接和交互时,否则,您将束缚Swing事件线程以及整个GUI。 Also use a SwingWorker for your background thread and just set its value which the JProgressBar can easily follow via a PropertyChangeListener.
也可以将SwingWorker用于后台线程,并只需设置其值即可通过PropertyChangeListener轻松地跟随JProgressBar。
while(!loginCompleted) {
signInProgressBar.setValue(5);
//System.out.println(p);
p++;
try {Thread.sleep(5000);}catch(Exception exc) {}
}
You made a small mistake signInProgressBar.setValue(5); 您犯了一个小错误signInProgressBar.setValue(5); should be ignInProgressBar.setValue(p);
应该是ignInProgressBar.setValue(p);
The current code when starts will generate a empty bar then immidieatly will set it to 50% , there is no statement that progresses it after login is complete. 当前代码在启动时会生成一个空条,然后立即将其设置为50%,没有语句在登录完成后继续执行。
You may need repaint
and invalidate
. 您可能需要
repaint
并使它们invalidate
。
try 尝试
signInProgressBar.invalidate();
signInProgressBar.repaint();
and why you set always 5 for signInProgressBar.setValue
以及为什么将
signInProgressBar.setValue
始终设置为signInProgressBar.setValue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.