2012-04-25 84 views
0

我想創建一個簡單的計時器與啓動停止和重置按鈕。我是使用線程和ActionListeners的新手。我有這個工作,並可以讓我的計時器開始,我的按鈕來改變從開始到停止的文本。但之後,我卡住了。我需要停止計時器,然後再次啓動它,如果我按下啓動按鈕。然後當然重置將它變回零。我不想使用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 ..」*那是什麼?大耳朵和關注度好的人? – 2012-04-25 20:54:53

+0

「lol」actionListeners .... – 2012-04-25 21:21:48

+0

如果你還沒有這樣做,你應該閱讀[Threads and Swing](http://java.sun.com/products/jfc/tsc/articles/threads/threads1。 html)和[併發在Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html)中有關多線程和Swing的很好介紹。 – 2012-04-25 22:40:52

回答

1

這是令人欽佩的,並且你想練和帶螺紋提高一個偉大的目標,但這真的不是競技場吧。問題在於Swing是單線程的 - 只有ui線程纔會更新圖形環境。

對於涉及圖形的操作,您應該使用javax.swing.Timer和javax.swing.SwingWorker,因爲這些是線程安全。從某種意義上說,你在這裏學習線程安全,所以你正在取得進展!

+0

好吧,讓我用javax.swing.Timer解決這個問題,我不想使用java.util.timer,我不知道有一個javax.swing.timer。 – 2012-04-25 20:58:05