원 소스 :
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Jishi extends JFrame implements ActionListener
{
JPanel north = new JPanel();
JPanel south = new JPanel();
JTextField tf1 = new JTextField("00",2);
JTextField tf2 = new JTextField("00",2);
JTextField tf3 = new JTextField("00",2);
JTextField tf4 = new JTextField("00",2);
JLabel label1 = new JLabel(":");
JLabel label2 = new JLabel(":");
JLabel label3 = new JLabel(":");
JButton b1 = new JButton("Start");
JButton b2 = new JButton("Stop");
JButton b3 = new JButton("Zero");
Font f = new Font("Courier New", Font.BOLD, 22);
boolean flag;
int ms = 0;
int s = 0;
int m = 0;
int h = 0;
long start;
long end;
public Jishi() {
this.setTitle("StopWatch");
north.add(tf1);
tf1.setFont(f);
north.add(label1);
north.add(tf2);
tf2.setFont(f);
north.add(label2);
north.add(tf3);
tf3.setFont(f);
north.add(label3);
north.add(tf4);
tf4.setFont(f);
this.add(north, "North");
south.add(b1);
south.add(b2);
south.add(b3);
this.add(south, "South");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(230, 110);
this.setLocation(400, 400);
this.setResizable(false);
this.setVisible(true);
}
public class counter extends Thread {
public void run() {
while (flag) {
end = System.currentTimeMillis();
ms = (int) ((end - start) / 10);
h = ms / (3600 * 100);
m = (ms - h * 3600 * 100) / (60 * 100);
s = (ms - h * 3600 * 100 - m * 60 * 100) / 100;
ms = ms - h * 3600 * 100 - m * 60 * 100 - s * 100;
tf4.setText(ms < 10 ? "0" + ms : "" + ms);
tf3.setText(s < 10 ? "0" + s : "" + s);
tf2.setText(m < 10 ? "0" + m : "" + m);
tf1.setText(h < 10 ? "0" + h : "" + h);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new Jishi();
}
public void actionPerformed(ActionEvent e) {
Object ob = e.getSource();
if (ob == b1) {
if (!flag) {
start = System.currentTimeMillis();
flag = true;
counter c = new counter();
c.start();
}
} else {
flag = false;
if (ob == b3){
tf1.setText("00");
tf2.setText("00");
tf3.setText("00");
tf4.setText("00");
}
}
}
}
------------------------------------------------------------------------------------------------------------------------------
06년에 한창 AWT , SWING 배울때 과제로 썼던 소스입니다.
참 간단하기 그지없지만 저한테는 첫 작품?이라고 할까.. 여튼 늘 보관해두었던 소스 입니다.
시간계산 부분은 지금봐도 한참 봐야 알겠네요.
역시 배울때 머리가 좋았다는 ^^;;