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

2016-02-19 18:56 0 1 收藏

人生本是一个不断学习的过程,在这个过程中,图老师就是你们的好帮手,下面分享的使用多线程技术让你的Swing及时响应各类事件懂设计的网友们快点来了解吧!

【 tulaoshi.com - 编程语言 】

1、使用线程例子

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

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、不使用线程的例子

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

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;
  
  }}
  
  总结


来源:https://www.tulaoshi.com/n/20160219/1619539.html

延伸阅读
下面我将对这两个问题和大家一起探讨一下。相信大家对生产者消费者问题并不生疏。在读书的时候我们采用系统体提供的p,v解决,这是对同一临界区资源同时进行读写需要的保护措施,本工程使用缓冲队列,故不需要对临界区进行加锁 。马上我会实现双缓存的版本。在此版本中我会实现对临界区的加减锁。 读取的数据要存储到相应的数...
多线程是一个比较轻量级的方法来实现单个应用程序内多个代码执行路径。 在系统级别内,程序并排执行,程序分配到每个程序的执行时间是基于该程序的所需时间和其他程序的所需时间来决定的。 然而,在每个程序内部,存在一个或者多个执行线程,它同时或在一个几乎同时发生的方式里执行不同的任务。 概要提示: iPhone中的线程应用并不是无...
Thread 创建线程的两种方法: 1、定义类继承Thread类,覆写类中的run方法,调用类对象的start方法,start方法启动线程,调用run方法。Thread类用于描述线程;该类定义一个功能run,用于存储线程要运行的代码。 2、定义类实现Runnable接口,覆盖Runnable接口中的方法,通过Thread类建立线程对象,将Runnable接口的子类对象作为实际参数传递给T...
我这里可以大概给你介绍一下,但对于每一种编程模型要看具体的示例是什么,而且我不可能给你罗列所有的代码,请谅解。 其实我们编程只要尽量站到比较高的层次,很多道理其实你会发现你已经懂了。 就多线程来说,我们开始设想只有两个线程(2时是不是算数学归纳法?)那么如果两个独立的线程会发生什么呢? 1。当一个线程进...
标签: 电脑入门
为什么没办法在 Windows 7 下加快 Windows 7 中文件复制/传输功能呢?可以用 robocopy 实现多线程文件复制实现。 经常进行文件管理操作的朋友们,提到复制/粘贴操作,想必很多人还会回想起当初 Vista 奇慢的复制速度,不满意于 Windows 系统内置的复制功能,因为它太龟速了。于是大家就使用 FastCopy、TeraCopy 之类的软件来加速复制,Windows ...

经验教程

452

收藏

82
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部