国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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

2019-11-17 06:00:17
字體:
來源:轉載
供稿:網友

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;

}}

總結



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 闽侯县| 金寨县| 筠连县| 霍州市| 新乐市| 九龙县| 饶平县| 自贡市| 博野县| 蒲城县| 米泉市| 师宗县| 浪卡子县| 凤翔县| 祁东县| 界首市| 宁津县| 汝城县| 白水县| 开化县| 土默特右旗| 寿阳县| 瓦房店市| 福贡县| 麻城市| 嵊泗县| 雅安市| 丰县| 铜陵市| 天镇县| 元谋县| 龙山县| 德江县| 金华市| 灵武市| 蓬溪县| 古田县| 棋牌| 佛山市| 宁蒗| 六安市|