您的位置:首页 > 其它

swing进度条

2015-06-01 22:58 267 查看
现在我们要做一个简单的界面。

包括一个进度条、一个输入框、开始和停止按钮。

需要实现的功能是:

当点击开始按钮,则更新进度条,并且在输入框内把完成的百分比输出(这里只做例子,没有真正去做某个工作)。

代码1:

[java]
view plaincopyprint?

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
public class SwingThreadTest1 extends JFrame {
private static final long serialVersionUID = 1L;
private static final String STR = "Completed : ";
private JProgressBar progressBar = new JProgressBar();
private JTextField text = new JTextField(10);
private JButton start = new JButton("Start");
private JButton end = new JButton("End");
private boolean flag = false;
private int count = 0;
public SwingThreadTest1() {
this.setLayout(new FlowLayout());
add(progressBar);
text.setEditable(false);
add(text);
add(start);
add(end);
start.addActionListener(new Start());
end.addActionListener(new End());
}

private void go() {
while (count < 100) {
try {
Thread.sleep(100);//这里比作要完成的某个耗时的工作
} catch (InterruptedException e) {
e.printStackTrace();
}
//更新进度条和输入框
if (flag) {
count++;
progressBar.setValue(count);
text.setText(STR + String.valueOf(count) + "%");
}
}
}
private class Start implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = true;//设置开始更新的标志
go();//开始工作
}
}
private class End implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = false;//停止
}
}
public static void main(String[] args) {
SwingThreadTest1 fg = new SwingThreadTest1();
fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fg.setSize(300, 100);
fg.setVisible(true);
}
}

import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JProgressBar;import javax.swing.JTextField;public class SwingThreadTest1 extends JFrame { private static final long serialVersionUID = 1L; private static final String STR = "Completed : "; private JProgressBar progressBar = new JProgressBar(); private JTextField text = new JTextField(10); private JButton start = new JButton("Start"); private JButton end = new JButton("End"); private boolean flag = false; private int count = 0; public SwingThreadTest1() { this.setLayout(new FlowLayout()); add(progressBar); text.setEditable(false); add(text); add(start); add(end); start.addActionListener(new Start()); end.addActionListener(new End()); } private void go() { while (count < 100) { try { Thread.sleep(100);//这里比作要完成的某个耗时的工作 } catch (InterruptedException e) { e.printStackTrace(); } //更新进度条和输入框 if (flag) { count++; progressBar.setValue(count); text.setText(STR + String.valueOf(count) + "%"); } } } private class Start implements ActionListener { public void actionPerformed(ActionEvent e) { flag = true;//设置开始更新的标志 go();//开始工作 } } private class End implements ActionListener { public void actionPerformed(ActionEvent e) { flag = false;//停止 } } public static void main(String[] args) { SwingThreadTest1 fg = new SwingThreadTest1(); fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fg.setSize(300, 100); fg.setVisible(true); }}

运行代码发现,

现象1当点击了开始按钮,画面就卡住了。按钮不能点击,进度条没有被更新,输入框上也没有任何信息。

原因分析:Swing是线程不安全的,是单线程的设计,所以只能从事件派发线程访问将要在屏幕上绘制的Swing组件。ActionListener的actionPerformed方法是在事件派发线程中调用执行的,而点击了开始按钮后,执行了go()方法,在go()里,虽然也去执行了更新组件的方法

progressBar.setValue(count);
text.setText(STR + String.valueOf(count) +"%");
但由于go()方法直到循环结束,它并没有返回,所以更新组件的操作一直没有被执行,这就造成了画面卡住的现象。

现象2过了一段时间(go方法里的循环结束了)后,画面又可以操作,并且进度条被更新,输入框也出现了我们想看到的信息。

原因分析:通过在现象1的分析,很容易联想到,当go()方法返回了,则其他的线程(更新组件)可以被派发了,所以画面上的组件被更新了。

(按钮事件已经是事件派发线程了,go()方法没有执行完,所以不会执行重绘组件的操作,如果想执行重绘组件的操作,可以把更新进度的方法放到另外的线程里面去,这样就不影响组件的重绘,但是另外的线程必须是单线程,否则)

为了让画面不会卡住,我们来修改代码,将耗时的工作放在一个线程里去做。

代码2:

[java]
view plaincopyprint?

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
public class SwingThreadTest2 extends JFrame {
private static final long serialVersionUID = 1L;
private static final String STR = "Completed : ";
private JProgressBar progressBar = new JProgressBar();
private JTextField text = new JTextField(10);
private JButton start = new JButton("Start");
private JButton end = new JButton("End");
private boolean flag = false;
private int count = 0;

GoThread t = null;
public SwingThreadTest2() {
this.setLayout(new FlowLayout());
add(progressBar);
text.setEditable(false);
add(text);
add(start);
add(end);
start.addActionListener(new Start());
end.addActionListener(new End());
}
private void go() {
while (count < 100) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (flag) {
count++;
System.out.println(count);
progressBar.setValue(count);
text.setText(STR + String.valueOf(count) + "%");
}
}
}
private class Start implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = true;
if(t == null){
t = new GoThread();
t.start();
}
}
}
//执行复杂工作,然后更新组件的线程
class GoThread extends Thread{
public void run() {
//do something...
go();
}
}
private class End implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = false;
}
}
public static void main(String[] args) {
SwingThreadTest2 fg = new SwingThreadTest2();
fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fg.setSize(300, 100);
fg.setVisible(true);
}
}

import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JProgressBar;import javax.swing.JTextField;public class SwingThreadTest2 extends JFrame { private static final long serialVersionUID = 1L; private static final String STR = "Completed : "; private JProgressBar progressBar = new JProgressBar(); private JTextField text = new JTextField(10); private JButton start = new JButton("Start"); private JButton end = new JButton("End"); private boolean flag = false; private int count = 0; GoThread t = null; public SwingThreadTest2() { this.setLayout(new FlowLayout()); add(progressBar); text.setEditable(false); add(text); add(start); add(end); start.addActionListener(new Start()); end.addActionListener(new End()); } private void go() { while (count < 100) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } if (flag) { count++; System.out.println(count); progressBar.setValue(count); text.setText(STR + String.valueOf(count) + "%"); } } } private class Start implements ActionListener { public void actionPerformed(ActionEvent e) { flag = true; if(t == null){ t = new GoThread(); t.start(); } } } //执行复杂工作,然后更新组件的线程 class GoThread extends Thread{ public void run() { //do something... go(); } } private class End implements ActionListener { public void actionPerformed(ActionEvent e) { flag = false; } } public static void main(String[] args) { SwingThreadTest2 fg = new SwingThreadTest2(); fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fg.setSize(300, 100); fg.setVisible(true); }}

我们执行了程序,结果和我们想要的一样,画面不会卡住了。

那这个程序是否没有问题了呢?

我们自定义了一个线程GoThread,在这里我们完成了那些耗时的工作,可以看作是“工作线程”,

而对于组件的更新,我们也放在了“工作线程”里完成了。

在这里,在事件派发线程以外的线程里设置进度条,是一个危险的操作,运行是不正常的。(对于输入框组件的更新是安全的。)

(在事件派发线程以外的线程里设置进度条,无法保证不会有多个线程去更新这个进度条,造成界面出现异常,因此,不要在事件派发线程以外的应用程序线程更新界面,可以用Swing invokes来更新界面)

只有从事件派发线程才能更新组件,根据这个原则,我们来修改我们现有代码。

代码3:

[b][java]
view plaincopyprint?[/b]

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class SwingThreadTest3 extends JFrame {
private static final long serialVersionUID = 1L;
private static final String STR = "Completed : ";
private JProgressBar progressBar = new JProgressBar();
private JTextField text = new JTextField(10);
private JButton start = new JButton("Start");
private JButton end = new JButton("End");
private boolean flag = false;
private int count = 0;

private GoThread t = null;

private Runnable run = null;//更新组件的线程
public SwingThreadTest3() {
this.setLayout(new FlowLayout());
add(progressBar);
text.setEditable(false);
add(text);
add(start);
add(end);
start.addActionListener(new Start());
end.addActionListener(new End());

run = new Runnable(){//实例化更新组件的线程
public void run() {
progressBar.setValue(count);
text.setText(STR + String.valueOf(count) + "%");
}
};
}
private void go() {
while (count < 100) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (flag) {
count++;
SwingUtilities.invokeLater(run);//将对象排到事件派发线程的队列中
}
}
}
private class Start implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = true;
if(t == null){
t = new GoThread();
t.start();
}
}
}

class GoThread extends Thread{
public void run() {
//do something...
go();
}
}
private class End implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = false;
}
}
public static void main(String[] args) {
SwingThreadTest3 fg = new SwingThreadTest3();
fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fg.setSize(300, 100);
fg.setVisible(true);
}
}

import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JProgressBar;import javax.swing.JTextField;import javax.swing.SwingUtilities;public class SwingThreadTest3 extends JFrame { private static final long serialVersionUID = 1L; private static final String STR = "Completed : "; private JProgressBar progressBar = new JProgressBar(); private JTextField text = new JTextField(10); private JButton start = new JButton("Start"); private JButton end = new JButton("End"); private boolean flag = false; private int count = 0; private GoThread t = null; private Runnable run = null;//更新组件的线程 public SwingThreadTest3() { this.setLayout(new FlowLayout()); add(progressBar); text.setEditable(false); add(text); add(start); add(end); start.addActionListener(new Start()); end.addActionListener(new End()); run = new Runnable(){//实例化更新组件的线程 public void run() { progressBar.setValue(count); text.setText(STR + String.valueOf(count) + "%"); } }; } private void go() { while (count < 100) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } if (flag) { count++; SwingUtilities.invokeLater(run);//将对象排到事件派发线程的队列中 } } } private class Start implements ActionListener { public void actionPerformed(ActionEvent e) { flag = true; if(t == null){ t = new GoThread(); t.start(); } } } class GoThread extends Thread{ public void run() { //do something... go(); } } private class End implements ActionListener { public void actionPerformed(ActionEvent e) { flag = false; } } public static void main(String[] args) { SwingThreadTest3 fg = new SwingThreadTest3(); fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fg.setSize(300, 100); fg.setVisible(true); }}

解释:SwingUtilities.invokeLater()方法使事件派发线程上的可运行对象排队。当可运行对象排在事件派发队列的队首时,就调用其run方法。其效果是允许事件派发线程调用另一个线程中的任意一个代码块。

还有一个方法SwingUtilities.invokeAndWait()方法,它也可以使事件派发线程上的可运行对象排队。

他们的不同之处在于:SwingUtilities.invokeLater()在把可运行的对象放入队列后就返回,而SwingUtilities.invokeAndWait()一直等待知道已启动了可运行的run方法才返回。如果一个操作在另外一个操作执行之前必须从一个组件获得信息,则应使用SwingUtilities.invokeAndWait()方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: