使用多线程技术让你的Swing及时响应各类事件
1、使用线程例子
package untitled1;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/
public class TestThread extends JFrame {
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton startButton = new JButton();
JButton stopButton = new JButton();
MyThread thread = null;
public TestThread() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1.setLayout(xYLayout1);
startButton.setText("start");
startButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
startButton_actionPerformed(e);
}
});
stopButton.setText("stop");
stopButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
stopButton_actionPerformed(e);
}
});
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(startButton, new XYConstraints(36, 105, 82, 30));
jPanel1.add(stopButton, new XYConstraints(160, 108, 100, 31));
}
void startButton_actionPerformed(ActionEvent e) {
if(thread != null) thread.stop();
thread = new MyThread();
thread.start();
}
void stopButton_actionPerformed(ActionEvent e) {
if(thread != null) thread.stop();
thread = null;
}
public static void main(String[] args)
{
TestThread test = new TestThread();
test.setSize(300,400);
test.show();
}
private class MyThread extends Thread
{
public MyThread(){
}
public void run(){
while(true){
try{
sleep(100);
}catch(InterruptedException e){}
System.out.println("this is a test!");
}
}
}
}
2、不使用线程的例子
package untitled1;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
public class NoThread extends JFrame
{
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JButton startButton = new JButton();
JButton stopButton = new JButton();
private boolean flagTrue = true;
public static void main(String[] args)
{
NoThread test = new NoThread();
test.setSize(300,400);
test.show();
}
public NoThread() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1.setLayout(xYLayout1);
startButton.setText("start");
startButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
startButton_actionPerformed(e);
}
});
stopButton.setText("stop");
stopButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
stopButton_actionPerformed(e);
}
});
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(startButton, new XYConstraints(27, 149, -1, -1));
jPanel1.add(stopButton, new XYConstraints(182, 151, -1, -1));
}
void startButton_actionPerformed(ActionEvent e) {
while(true){
try{
Thread.currentThread().sleep(100);
}catch(InterruptedException er){}
if(flagTrue){
System.out.println("this is a test!");
}
}
}
void stopButton_actionPerformed(ActionEvent e) {
if(flagTrue) flagTrue = false;
else flagTrue = true;
}
}
总结:
在不使用线程的例子中,当点击start按钮后,整个Frame将不再相应任何鼠标事件(如
点击stop、点击关闭按钮);
而在使用线程的例子中,点击start按钮启动线程后,Frame界面可以很好的相应其它事
件(如点击stop,就可以将该线程停止,点击关闭按钮则可以退出该程序)
|