現在
//: Counter
// A responsive user interface with threads
import java
import java
import java
class SeparateSubTask extends Thread {
private int count =
private Counter
private boolean runFlag = true;
public SeparateSubTask(Counter
this
start();
}
public void invertFlag() { runFlag = !runFlag;}
public void run() {
while (true) {
try {
sleep(
} catch (InterruptedException e){}
if(runFlag)
c
}
}
}
public class Counter
TextField t = new TextField(
private SeparateSubTask sp = null;
private Button
onOff = new Button(
start = new Button(
public void init() {
add(t);
start
add(start);
onOff
add(onOff);
}
class StartL implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(sp == null)
sp = new SeparateSubTask(Counter
}
}
class OnOffL implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(sp != null)
sp
}
}
public static void main(String[] args) {
Counter
Frame aFrame = new Frame(
aFrame
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System
}
});
aFrame
aFrame
applet
applet
aFrame
}
}
現在
SeparateSubTask類是對Thread的一個簡單擴展
按下onOff按鈕
用內部類改善代碼
下面說說題外話
//: Counter
// Counter
import java
import java
import java
public class Counter
private class SeparateSubTask extends Thread {
int count =
boolean runFlag = true;
SeparateSubTask() { start(); }
public void run() {
while (true) {
try {
sleep(
} catch (InterruptedException e){}
if(runFlag)
t
}
}
}
private SeparateSubTask sp = null;
private TextField t = new TextField(
private Button
onOff = new Button(
start = new Button(
public void init() {
add(t);
start
add(start);
onOff
add(onOff);
}
class StartL implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(sp == null)
sp = new SeparateSubTask();
}
}
class OnOffL implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(sp != null)
sp
}
}
public static void main(String[] args) {
Counter
Frame aFrame = new Frame(
aFrame
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System
}
});
aFrame
aFrame
applet
applet
aFrame
}
}
這個SeparateSubTask名字不會與前例中的SeparateSubTask沖突——即使它們都在相同的目錄裡——因為它已作為一個內部類隱藏起來
此外
無論在什麼時候
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27450.html