주간 베스트 월간 베스트 3개월 베스트 베스트 게시물
연길시 신화서점 조선말 도서

[java] 간단한 스톱워치 만들기

신규가입 | 2011.03.15 18:54:33 댓글: 0 조회: 10193 추천: 0
분류웹 프로그래밍 https://life.moyiza.kr/itstudy/1760250
원 소스 :

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 배울때 과제로 썼던 소스입니다. 
참 간단하기 그지없지만 저한테는 첫 작품?이라고 할까.. 여튼 늘 보관해두었던 소스 입니다.
시간계산 부분은 지금봐도 한참 봐야 알겠네요. 
역시 배울때 머리가 좋았다는 ^^;;
추천 (0) 선물 (0명)
IP: ♡.161.♡.5
3,006 개의 글이 있습니다.
제목 글쓴이 날짜 조회
관리자
2003-09-20
11766
관리자
2003-09-20
11302
관리자
2003-09-20
20376
지구인
2010-08-27
19242
지구인
2009-09-07
13538
SOLIDH
2010-01-29
15519
엔죠라이프
2004-10-07
16621
혁이사랑
2011-06-16
7849
큐큐100
2011-05-30
4883
nero
2011-04-20
5018
Big샤넬
2011-04-05
4100
Big샤넬
2011-04-05
3810
Big샤넬
2011-04-05
3585
지구인
2011-04-02
3788
내다
2011-03-31
4321
retomato
2011-03-22
4360
신규가입
2011-03-15
10193
에스G
2011-02-21
3535
에스G
2011-02-21
3456
에스G
2011-02-21
3664
에스G
2011-02-21
2965
에스G
2011-02-21
4518
자이홍
2011-02-01
6030
필수비타민
2011-01-31
2930
세뇨리타
2011-01-24
6058
세뇨리타
2011-01-23
7708
세뇨리타
2011-01-22
5452
세뇨리타
2011-01-22
4930
모이자 모바일