2017-03-20 20 views
1

您好日子我想創建一個「重置按鈕」,其中我的定時器將重置。我創建了一個新按鈕並將其命名爲「重置」,並使用代碼「tm2.restart();」但它不在我創建的新按鈕中工作。這是我的代碼:如何在Java GUI中重置定時器並在停止後顯示消息

import javax.swing.Timer; 
public class deploy extends JFrame { 

private int seconds; 
private SimpleDateFormat df; 
private boolean isRunning; 
private JLabel lblTimer1; 
private JButton btnStart1; 

public deploy() { 

lblTimer1 = new JLabel("New label"); 
lblTimer1.setForeground(Color.WHITE); 
lblTimer1.setFont(new Font("Tahoma", Font.PLAIN, 20)); 
lblTimer1.setBounds(100, 231, 94, 16); 
contentPane.add(lblTimer1); 

Timer tm2 = new Timer(1000, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       setTimer(); 
       seconds++; 
      } 
     }); 

btnStart1 = new JButton("Start"); 
btnStart1.setBackground(Color.LIGHT_GRAY); 
btnStart1.setForeground(Color.BLUE); 
btnStart1.addActionListener(new ActionListener() { 
    @Override 
      public void actionPerformed(ActionEvent e) { 

       if(isRunning) { 
        tm2.stop(); 
        btnStart1.setText("Start"); 
       }else { 
        tm2.start(); 
        btnStart1.setText("Stop"); 
       } 

       isRunning = !isRunning; 
      } 
     }); 

,因爲我創造了「網吧管理應用程序」,客戶會走在和我這種計時器的格式爲「的SimpleDateFormat」(00:00:00)記錄他/她的時間,直到他/她退出並顯示消息他/她將被支付的金額。請幫忙。感謝

+1

我沒有看到'tm2.restart()'在該代碼......請提供[MCVE。 – Fildor

+1

順便說一下,使用Timer跟蹤時間範圍並不是最好的想法......您可以簡單地記錄2個時間戳(在登錄時,在註銷時)並在需要時計算差異。 – Fildor

+0

@Fildor我不包括格式爲tm2.restart()的代碼;我刪除,因爲它不工作.. –

回答

1

審查代碼,不要猶豫,問是什麼還不清楚:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.TimeZone; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.border.EmptyBorder; 
public class Deploy extends JFrame { 

    private int seconds; 
    private SimpleDateFormat df; 
    private JLabel lblTimer; 
    private Timer timer; 
    private JButton startButton; 

    public Deploy() { 

     JPanel contentPane = new JPanel(); 
     contentPane.setBackground(Color.DARK_GRAY); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(new BorderLayout()); 

     lblTimer = new JLabel(); 
     lblTimer.setForeground(Color.WHITE); 
     lblTimer.setFont(new Font("Tahoma", Font.PLAIN, 20)); 
     lblTimer.setPreferredSize(new Dimension(100,30)); 
     contentPane.add(lblTimer,BorderLayout.NORTH); 

     timer = new Timer(1000, new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       setTimer(); 
       seconds++; 
      } 
     }); 

     JPanel buttonsPanel = new JPanel(); 
     contentPane.add(buttonsPanel, BorderLayout.SOUTH); 

     startButton = new JButton("Start"); 
     startButton.setBackground(Color.LIGHT_GRAY); 
     startButton.setForeground(Color.BLUE); 
     startButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 

       if(timer.isRunning()) { 
        timer.stop(); 
        startButton.setText("Start"); 
       }else { 
        timer.start(); 
        startButton.setText("Stop"); 
       } 
      } 
     }); 

     startButton.setPreferredSize(new Dimension(100,30)); 
     buttonsPanel.add(startButton); 

     JButton resetButton = new JButton("Reset"); 
     resetButton.setBackground(Color.LIGHT_GRAY); 
     resetButton.setForeground(Color.RED); 
     resetButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       resetTimer(); 
      } 
     }); 
     resetButton.setPreferredSize(new Dimension(100,30)); 
     buttonsPanel.add(resetButton); 

     df = new SimpleDateFormat("HH:mm:ss"); // HH for 0-23 
     df.setTimeZone(TimeZone.getTimeZone("GMT")); 

     resetTimer(); 
     pack(); 
     setVisible(true); 
    } 

    private void setTimer() { 
     Date d = new Date(seconds * 1000L); 
     String time = df.format(d); 
     lblTimer.setText(time); 
    } 

    private void resetTimer() { 

     if(timer.isRunning()) { 
      timer.stop(); 
     } 
     startButton.setText("Start"); 
     seconds = 0; 
     setTimer(); 
    } 

    public static void main(String[] args) { 
     new Deploy(); 
    } 
} 
+0

它現在的作品..謝謝 –

相關問題