使用多线程技术让你的Swing及时响应各类事件

豆豆网   技术应用频道   2006年07月14日  【字号: 收藏本文

本文详细介绍使用多线程技术让你的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 javax/swing/JFrame.java.html" target="_blank">JFrame {
  javax/swing/JPanel.java.html" target="_blank">JPanel jPanel1 = new javax/swing/JPanel.java.html" target="_blank">JPanel();
  XYLayout xYLayout1 = new XYLayout();
  javax/swing/JButton.java.html" target="_blank">JButton startButton = new javax/swing/JButton.java.html" target="_blank">JButton();
  javax/swing/JButton.java.html" target="_blank">JButton stopButton = new javax/swing/JButton.java.html" target="_blank">JButton();
  MyThread thread = null;
  public TestThread() {
   try {
    jbInit();
   }
   catch(java/lang/Exception.java.html" target="_blank">Exception e) {
    e.printStackTrace();
   }
  }
  private void jbInit() throws java/lang/Exception.java.html" target="_blank">Exception {
   jPanel1.setLayout(xYLayout1);
   startButton.setText("start");
   startButton.addActionListener(new java.awt.event.java/awt/event/ActionListener.java.html" target="_blank">ActionListener() {
    public void actionPerformed(java/awt/event/ActionEvent.java.html" target="_blank">ActionEvent e) {
     startButton_actionPerformed(e);
    }
   });
   stopButton.setText("stop");
   stopButton.addActionListener(new java.awt.event.java/awt/event/ActionListener.java.html" target="_blank">ActionListener() {
    public void actionPerformed(java/awt/event/ActionEvent.java.html" target="_blank">ActionEvent e) {
     stopButton_actionPerformed(e);
    }
   });
   this.getContentPane().add(jPanel1, java/awt/BorderLayout.java.html" target="_blank">BorderLayout.CENTER);
   jPanel1.add(startButton, new XYConstraints(36, 105, 82, 30));
   jPanel1.add(stopButton, new XYConstraints(160, 108, 100, 31));
  }
  void startButton_actionPerformed(java/awt/event/ActionEvent.java.html" target="_blank">ActionEvent e) {
         if(thread != null) thread.stop();
         thread = new MyThread();
         thread.start();
  }
  void stopButton_actionPerformed(java/awt/event/ActionEvent.java.html" target="_blank">ActionEvent e) {
         if(thread != null) thread.stop();
         thread = null;
  }
    public static void main(java/lang/String.java.html" target="_blank">String[] args)
   {
     TestThread test = new TestThread();
     test.setSize(300,400);
     test.show();
   }
     private class MyThread extends java/lang/Thread.java.html" target="_blank">Thread
   {
     public MyThread(){
     }
     public void run(){
       while(true){
         try{
           sleep(100);
         }catch(java/lang/InterruptedException.java.html" target="_blank">InterruptedException e){}
         java/lang/System.java.html" target="_blank">System.out.println("this is a test!");
       }
     }
   }
}

责编:豆豆技术应用

正在加载评论...