2007年5月30日水曜日

CountdownTimer





// CountdownTimer.java

import java.awt.*;
import java.awt.event.*;
import java.lang.*;

class CountdownTimer extends Frame{
Button b1;
Button b2;
TextField ent1;
String input;
int count;
Thread thread=null;

class CountDown implements Runnable{ // Runnableを実装したクラス
public void run(){
while((count>=0) && (thread!=null)){
input=Integer.toString(count);
ent1.setText(input);
try{
Thread.sleep(1000);
}catch(Exception e1){}
count--;
}
}
}

public CountdownTimer(){
setLayout(new FlowLayout());

add(b1=new Button("START")); // startメソッドをオーバーライド
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e2){
input=ent1.getText();
count=Integer.parseInt(input);
thread=new Thread(new CountDown());
thread.start();
}
});

add(b2=new Button("STOP")); // stopメソッドをオーバーライド
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e3){
thread=null;
}
});

add(ent1=new TextField("0",10));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e4){
System.exit(0);
}
});
}

public static void main(String args[]){
CountdownTimer gui=new CountdownTimer();
gui.pack();
gui.setVisible(true);
}
}


 こんなかんじかな。てか先週は運動会で休講だったから、これ今週の課題なんだ~提出後に知ったよ。うぅ、授業前にやって損した。もう寝る。

2 件のコメント:

  1. あ、プロ演Bって今年課題変わったんだね~。

    返信削除
  2. MLの授業が増えたんです。MLってやる意味あるのかな。
    今セメこそ過去問のために長老経由でアドレスを聞くかと思いますが、よろしくです。

    返信削除