⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clockpanel.java.bak

📁 秒表计时
💻 BAK
字号:
//********************************************************************
//  ClockPanel.java       Author: 钟凯艺
//
//  为左pp8.10跑表的时间显示写的
//********************************************************************

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ClockPanel extends JPanel
{  private int count;
	 
   private JButton clockButton;
   private JButton ZeroButton;
   
   private JLabel label;
   
   private int flag = 1; 
   private JLabel Label;

   private Timer timer;
   
   private final int DELAY = 16;

   //-----------------------------------------------------------------
   //  Constructor: Sets up the main GUI components.
   //-----------------------------------------------------------------
   public ClockPanel()
   
   {  count = 0;
   	  clockButton = new JButton ("启动");    
      clockButton.addActionListener (new ButtonListener());
      
      label = new JLabel (" ");
      ZeroButton = new JButton ("清零");
      ZeroButton.addActionListener (new ZeroListener());

     

      add (clockButton);
      add (ZeroButton);
      add (label);
      

      setPreferredSize (new Dimension(300, 75));
      setBackground (Color.yellow);

   }
   
   private class ButtonListener implements ActionListener
   
   {   
   	public void actionPerformed (ActionEvent event)
      
     {
      	if(flag == 1) 
        {      	 
         clockButton.setText("停止");
      	 timer = new Timer(DELAY, new ClockListener()); 
      	 timer.start(); 
      	 
      	 flag = 0;
      	}
      	
        else 
      	
        {
      	 clockButton.setText("启动");
      	 timer.stop(); 
      	 flag = 1;
      		}
     }
    }
   
   private class ClockListener implements ActionListener
   {
   	
      
      public void actionPerformed (ActionEvent event)
      {
      count++;
              
      label.setText((count/3600)%60+"分"+(count/60)%60+"秒"+count%60);
         
      repaint();             
      }
      
  
}

private class ZeroListener implements ActionListener
   {
   	
      
      public void actionPerformed (ActionEvent event)
      {   
      
       timer.stop();
       label.setText("0分0秒");
       clockButton.setText("重新启动");
      
       repaint();                          
      }
      
  
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -