开发学院WEB开发Jsp 使用多线程技术让你的Swing及时响应各类事件 阅读

使用多线程技术让你的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:

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;

}}

总结


Tags:使用 线程 技术

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接