2012-04-25 3 views
0

시작 중지 및 재설정 단추가있는 간단한 타이머를 만들려고합니다. 나는 Threads와 ActionListeners를 처음 사용한다. 이 작업을 시작하고 타이머를 시작하고 내 단추를 사용하여 텍스트를 처음부터 끝까지 변경할 수 있습니다. 하지만 그 후 나는 붙어있다. 타이머를 멈추고 시작 버튼을 누르면 다시 시작해야합니다. 그런 다음 다시 초기화하면 다시 0으로 바뀝니다. java.util.Timer을 사용하고 싶지 않습니다. 단지 스레드를 사용하고 싶습니다. 한 번 시작된 스레드를 일시 중지했다가 다시 시작하는 방법은 무엇입니까? 내장 된 메서드를 사용하여 시도했지만 컴파일 할 수 없습니다.스레드 JButton 시작 및 중지

import javax.swing.*; 
import java.awt.Font; 
import java.lang.String; 
import java.awt.*; 

public class Timer extends JPanel { 

    // Here are the fields from below! 

    private static JLabel label = new JLabel(" 0.00 seconds"); 
    private static javax.swing.JButton start = new javax.swing.JButton("Start"); 
    private static javax.swing.JButton reset = new javax.swing.JButton("reset"); 

    /** 
    * Here is the Timer method- Begins with JLabel with medium alignment. 
    */ 
    public Timer() { 
    //new Thread(this).start(); 
    //label = new JLabel(" 0.00 Seconds"); 
    //this.label = label; 
    reset(); 
    } 


    /** 
    * Here is the Reset method- pressing this button from below will 
    * stop the thread and reset the text. 
    */ 
    public static void reset() { 
    label.setFont(new Font("Arial",Font.BOLD,36)); 
    label.setText(" 0.00 Seconds"); 

    } 

    public static void startStop() { 
    //start.setText("stop"); 
    //validate(); 

    } 

    public static void countDown() { 
    int Msec=0,min=0,sec=0; 
    while(sec < 60) { 
      label.setText(min+":"+sec+":"+Msec); 
      //label.setLayout(new BorderLayout.CENTER); 
      //label. 
      Msec++; 
      if(Msec==60) { 
      Msec=0; 
      sec++; 
      //label.setText(min+":"+sec+":"+Msec); 
      } 
      if(sec ==60) { 
      Msec =0; 
      sec = 0; 
      min++; 
      } 
      try 
     { 
      Thread.sleep(10); 
     } 
     catch(Exception e) 
     {} 
     } 
    }  

public static void main(String [] args) { 

    // we need a JFrame to put these elements into 
    javax.swing.JFrame win = new javax.swing.JFrame("Timer"); 
    // here we are instantating a new timer 
    final Timer time = new Timer(); 

    //Annonymous inner class 
    start.addActionListener(new java.awt.event.ActionListener() { 
    // here is the action performed method to start this. 
    public void actionPerformed(java.awt.event.ActionEvent e) { 
     //here we are creating a new thread to run throwable 
     // every click creates a new Thread (so it goes faster) 
     String text = (String)e.getActionCommand(); 
     if (text.equals("Start")){ 
     start.setText("Stop"); 
     } 
     else{ 
     start.setText("Start"); 
     } 
     Thread restart = new Thread(new Runnable() { 
     public void run() { 
      countDown(); 
      //startStop(); 
     } 

     }); 
     restart.start(); 
    } 
    }); 


    //Look at the below abstract actionlistener below to get reset to work 
    javax.swing.JButton reset = new javax.swing.JButton("reset"); 

    // here we are creating a new annonomys inner class.... check the 
    // curly braces 
    reset.addActionListener(new java.awt.event.ActionListener() { 
    public void actionPerformed(java.awt.event.ActionEvent e) { 
     Thread restart = new Thread(new Runnable() { 
     public void run() { 
      reset(); 
      //Thread.stop(); 
     } 

     }); 
     restart.destroy(); 
    } 
    }); 


    //label.setVisible(true); 

    javax.swing.JPanel tb = new javax.swing.JPanel(); 
    tb.add(reset); 
    tb.add(start); 
    //tb.add(circle); 
    win.add(tb,java.awt.BorderLayout.NORTH); 
    win.setSize(300,300); 
    //win.add(tb); 
    win.add(label,java.awt.BorderLayout.CENTER); 

    win.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE); 
    // hiding inside setVisible is borderLayout 
    win.setVisible(true); 
} 
} 
+0

* "ActiveListeners ..를 사용하는 것이 처음입니다."* 저게 뭐지? 큰 귀와 좋은주의 집중력을 가진 사람들? –

+0

"lol"actionListeners .... –

+0

아직 작성하지 않았다면 [스레드 및 스윙] (http://java.sun.com/products/jfc/tsc/articles/threads/threads1)을 읽어야합니다. html) 및 [Concurrency in Swing] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html)에서 멀티 스레딩 및 스윙에 대해 잘 설명합니다. –

답변

1

스레드로 연습하고 개선하고 싶다는 위대한 목표는 훌륭하지만 훌륭한 목표입니다.하지만 실제로는이 분야가 아닙니다. 문제는 Swing이 단일 스레드라는 것입니다. ui 스레드 만 그래픽 환경을 업데이트해야합니다.

그래픽과 관련된 작업을 수행하려면 스레드 안전 인 javax.swing.Timer 및 javax.swing.SwingWorker를 사용해야합니다. 한 가지 방법으로 여기 스레드 안전성에 대해 배우고 있으므로 진전을 이루고 있습니다!

+0

자,이 작업을 javax.swing.Timer를 사용하여 해보겠습니다. java.util.timer를 사용하고 싶지 않았습니다. javax.swing.timer가 있다는 것을 알지 못했습니다. –