Java相談室 Part3

このエントリーをはてなブックマークに追加
669ナナシ
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class test extends Applet implements Runnable,ActionListener{
Thread MyThread;
int count;
Graphics offGraphics;
Label lb;

public void init(){
String c = new String();
count = 0;
Panel p = new Panel();
Button b = new Button("OK");
b.addActionListener(this);
p.add(b);
setLayout(new BorderLayout());
add("South",p);
lb = new Label("0");
lb.setAlignment(Label.RIGHT);
add("North",lb);
}

public void paint(Graphics g){
String c = new String();
lb.setText(c.valueOf(count));
}

public void run(){
Thread thisThread = Thread.currentThread();
while(MyThread == thisThread){
up();
try{
MyThread.sleep(1000);
}catch(InterruptedException e){
break;
}

}
}
670ナナシ:2001/07/30(月) 21:08
public void up(){
count++;
if(count > 1000){
count = 0;
}
repaint();
}

public void actionPerformed(ActionEvent e){
String arg = e.getActionCommand();
String c = new String();
if(arg.equals("OK")){
if(MyThread == null){
MyThread = new Thread(this);
MyThread.start();
count = 0;
}else{
MyThread = null;
for(int i = 0;i < 5;i++){
up();
try{
Thread.sleep(1000);
}catch(InterruptedException e1){
break;
}
}
lb.setText(c.valueOf(count));
}
}
}

public void update(Graphics g){
paint(g);
}
}