使用多线程技术让你的Swing及时响应各类事件
2008-01-05 20:28:50 来源:WEB开发网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;
}}
总结
- ››使用脚本恢复WinXP系统的用户登录密码
- ››使用phpMyadmin创建数据库及独立数据库帐号
- ››使用Zend Framework框架中的Zend_Mail模块发送邮件...
- ››使用cout标准输出如何控制小数点后位数
- ››使用nofollow标签做SEO的技巧
- ››使用 WebSphere Message Broker 的 WebSphere Tra...
- ››使用SQL Server事件探查器做应用程序的性能分析
- ››使用SQL Server事件探查器分析死锁原因
- ››使用纯文本文件打造WCF服务
- ››使用 Dojo 开发定制 Business Space 小部件,第 4...
- ››使用 ADDRESS 与 INDIRECT函数查询信息
- ››使用 COLUMN函数编制单元信息
更多精彩
赞助商链接