程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> 使用多線程技術讓你的Swing及時響應各類事件

使用多線程技術讓你的Swing及時響應各類事件

編輯:JAVA編程入門知識

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;
  
  }}
  
  總結


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved