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

📄 timertest.java

📁 java多线程生成时钟用户可自定义时间 界面为钟面
💻 JAVA
字号:
import java.awt.*; 
import javax.swing.*; 
import java.util.*; 
import java.awt.event.*; 

public class TimerTest 
{ 

 public static void main(String[] args) 
 { 
 
 MyTimer1 f = new MyTimer1(); 
 f.show(); 

 } 
} 

class MyTimer1 extends JFrame implements ActionListener
{ 
static int count=0; //判断是否重定义了时间 
JButton set,exit,recover;

public MyTimer1() 
{
setSize(800, 600); 

setTitle(" TimerTest "); 

Container c = getContentPane(); 
c.add(new ClockCanvas("Made By Qmei", "GMT+8"),BorderLayout.CENTER); 

JPanel mypanel;
mypanel=new JPanel();

set=new JButton("时钟设置");
set.addActionListener( this );
mypanel.add(set);

recover=new JButton("系统时间");
recover.addActionListener( this );
mypanel.add(recover);

exit=new JButton("退出时钟");
exit.addActionListener( this );
mypanel.add(exit);

GridLayout g1;
g1=new GridLayout(3,1);
mypanel.setLayout(g1);


c.add(mypanel,BorderLayout.WEST);
} 
 
 public void actionPerformed( ActionEvent event )
   {  
     if ( event.getSource() == set)
     {   
         MyTimer myTimer=new MyTimer();
     	 myTimer.setCurrentTime(); }
                
     	 
      if ( event.getSource() == exit)
     {  
     	  System.exit(0); }   	 
      if ( event.getSource() == recover)
     {   
          MyTimer myTimer=new MyTimer();
          myTimer.setCurrentTimeAsSystemTime() ;}	 
   }

} 

//定义接口 
interface TimerListener1 
{ 
void timeElapsed(Timer1 t); 
} 

class Timer1 extends Thread 
{ 
private TimerListener1 target; 
private int interval; 

public Timer1(int i, TimerListener1 t) 
{ 
target = t; 
interval = i; 
setDaemon(true); 
} 

public void run() 
{ try 
{ 
while (!interrupted()) 
{ 
sleep(interval); 
target.timeElapsed(this); 
} 
} 
catch(InterruptedException e) {} 
} 
} 

class ClockCanvas extends JPanel  implements TimerListener1 
{  
static int seconds = 0; 
private String city; 

private GregorianCalendar calendar; 
      
public ClockCanvas(String c, String tz) 
{ 
city = c; 
calendar = new GregorianCalendar(TimeZone.getTimeZone(tz)); 
Timer1 t = new Timer1(1000, this); 
t.start(); 
setSize(180, 180); 
} 



//绘制钟面 
public void paintComponent(Graphics g) 
{ 
super.paintComponent(g); 

g.setColor(Color.gray);
g.drawOval(100, 5, 500, 500); 

g.setColor(Color.PINK);
g.drawOval(135, 40, 430, 430); 
g.drawOval(136, 41, 428, 428);

g.fillOval(340, 60, 15,15);//12 
g.fillOval(340, 430, 15,15);//6 
g.fillOval(150, 248, 15,15); //9
g.fillOval(530, 248, 15,15);//3 

g.fillOval(250,100, 10,10); 
g.fillOval(190, 170, 10,10); 
g.fillOval(440,100, 10,10); 
g.fillOval(500, 170, 10,10); 
g.fillOval(190,320, 10,10); 
g.fillOval(250, 390, 10,10); 
g.fillOval(500,320, 10,10); 
g.fillOval(440, 390, 10,10); 


Font d=new Font("幼圆",Font.BOLD,20); 
g.setFont(d); 
g.setColor(Color.CYAN);
g.drawString("12", 340, 120) ;
g.drawString("3", 490, 258) ;
g.drawString("6", 340, 400) ;
g.drawString("9", 190, 258) ;

double hourAngle = 2 * Math.PI * (seconds - 3 * 60 * 60) / (12 * 60 * 60); 
double minuteAngle = 2 * Math.PI * (seconds - 15 * 60) / (60 * 60); 
double secondAngle = 2 * Math.PI * (seconds - 15) / 60; 

 
g.setColor(Color.RED);
g.drawLine(349, 249, 349 + (int)(40* Math.cos(hourAngle)), 
250 + (int)(40 * Math.sin(hourAngle))); 
g.drawLine(350, 250, 350 + (int)(40* Math.cos(hourAngle)), 
250 + (int)(40 * Math.sin(hourAngle))); 
g.drawLine(350, 251, 350 + (int)(40* Math.cos(hourAngle)), 
250 + (int)(40 * Math.sin(hourAngle)));
g.drawLine(350, 252, 350 + (int)(40* Math.cos(hourAngle)), 
250 + (int)(40 * Math.sin(hourAngle)));
 
 
g.setColor(Color.BLACK);
g.drawLine(350, 250, 350 + (int)(70* Math.cos(minuteAngle)), 
250 + (int)(70 * Math.sin(minuteAngle))); 
g.drawLine(350, 251, 350 + (int)(70* Math.cos(minuteAngle)), 
250 + (int)(70 * Math.sin(minuteAngle))); 

g.setColor(Color.gray);
g.drawLine(350, 250, 350 + (int)(110* Math.cos(secondAngle)), 
250+ (int)(110 * Math.sin(secondAngle)));

g.setColor(Color.GRAY);
g.fillOval(338, 238, 25,25);
 g.setColor(Color.DARK_GRAY);
g.fillOval(345, 245, 10,10); 
 
g.setColor(Color.PINK);
Font f=new Font("幼圆",Font.BOLD,25); 
g.setFont(f); 
g.drawString(city, 290, 350); 
} 


public void timeElapsed(Timer1 t) 
{ 
calendar.setTime(new Date()); 
if(MyTimer1.count!=0) {int a=1; 

seconds=MyTimer.intHour*60*60+MyTimer.intMinute*60+MyTimer.intSecond; 
seconds+=a;


repaint();} 
else 

{ seconds = calendar.get(Calendar.HOUR) * 60 * 60 
+ calendar.get(Calendar.MINUTE) * 60 
+ calendar.get(Calendar.SECOND); 
repaint();} 
} 
} 


//定义时钟类
class MyTimer 
implements TimerListener 
{ 
static int intHour,intMinute,intSecond; 

public MyTimer() 
{ 
setCurrentTimeAsSystemTime(); 
Timer t = new Timer(1000, this);  //实例Timer类,里面有run方法 
t.start(); 
} 


//设定当前时间
public void setCurrentTime() 
{ 
String strTemp=JOptionPane.showInputDialog(null,"请输入时(24小时)"); 
int iHour=Integer.parseInt(strTemp); 

strTemp=JOptionPane.showInputDialog(null,"请输入分"); 
int iMinute=Integer.parseInt(strTemp); 

strTemp=JOptionPane.showInputDialog(null,"请输入秒"); 
int iSecond=Integer.parseInt(strTemp); 

//设定当前时间为对话框输入的时间 
if(iHour>=0&&iHour<24) 
intHour=iHour; 
else intHour=0; 
if(iMinute>=0&&iMinute<60) 
intMinute=iMinute; 
else intMinute=0; 
if(iSecond>=0&&iSecond<60) 
intSecond=iSecond; 
MyTimer1.count++; 


} 

//设定当前时间为系统时间,构造函数调用 
public void setCurrentTimeAsSystemTime() 
{  
Date timeCurrent=new Date(); 
 
intHour=timeCurrent.getHours(); 

intMinute=timeCurrent.getMinutes(); 

intSecond=timeCurrent.getSeconds(); 
} 

public void timeElapsed(Timer t) 
{ 

intSecond++; 
if (intSecond==60) 
{ 
intMinute++; 
intSecond=0; 
} 

if (intMinute==60) 
{ 
intHour++; 
intMinute=0; 
} 

if (intHour==24) 
{ 
intHour=0; 
} 

} 
} 

interface TimerListener 
{ 
void timeElapsed(Timer t); 
} 

class Timer extends Thread 
{ 
private TimerListener target; 
private int interval; 

public Timer(int i, TimerListener t) 
{ 
target = t; 
interval = i; 
setDaemon(true); 
} 

public void run() 
{ try 
{ 
while (!interrupted()) 
{ 
sleep(interval); 
target.timeElapsed(this); 
} 
} 
catch(InterruptedException e) {} 
} 
}

⌨️ 快捷键说明

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