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()方法。
包括一个进度条、一个输入框、开始和停止按钮。
需要实现的功能是:
当点击开始按钮,则更新进度条,并且在输入框内把完成的百分比输出(这里只做例子,没有真正去做某个工作)。
代码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()方法。
相关文章推荐
- LeetCode 03: Longest Substring Without Repeating Characters
- cdoj 71 I am Lord Voldemort 水题
- RMI+JDBC远端数据库的访问
- LINK : fatal error LNK1104: cannot open file "mfc42ud.lib"解决方法
- JavaScript Window Location
- netty 的 Google protobuf 开发
- 整合apache与php
- [1] MQTT,mosquitto,Eclipse Paho----入门
- 如何使用 AngularJS 的 ngShow 和 ngHide
- 业务现状描述
- LeetCode 8:String to Integer (atoi)
- Hive简介
- 让自己的过去现在将来掌握在自己手中
- [016] 如何使用真机调试Android应用
- JavaScript Window Screen
- 第二次冲刺(6)
- Java 学习总结(一)
- java 把DataTable数据类型转换为树形结构(多叉树)
- 在此记录学习和工作经验
- Linux常用的20条命令