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

📄 clockly.java

📁 非Applet的java电子时钟与秒表小程序 包括多线程控制与界面切换
💻 JAVA
字号:
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;



public class Clock extends Frame implements ActionListener, ItemListener
{static Panel p1,p2,p3;
 static Label l1,l2,l3;
 static Thread t1,t2;
 static Choice tim, minut;
 static Button alarm;
 static Button switcht,reset,start,stop1;
 static Frame f,f2;
 static long num=1;
 static long current;
 static String ll; 
 static int x=1,y=0;
 static int hours,minutes,alert,j;
 public static String type(int number)
  { String l;
    l=String.valueOf(number);
    if(l.length()==1)
    l="0"+l;
     return l;
    
  
     }
 static void setalarm()
 { 
   if(num%2==1)
     {alarm.setLabel("alarm off");
      num--;alert=1;
      alarm.setForeground(Color.red);
      l1.setForeground(Color.red);
      String str=type(hours)+":"+type(minutes);
      l1.setText(str);
     }
   else if(num%2==0)
     {alarm.setLabel("alarm on");
      num++;alert=0;
      alarm.setForeground(Color.green);
      l1.setForeground(Color.green);l1.setText("alarm off");
     }
 }
 static void setswitcht()
 { --x;
   
   f.setVisible(false);
   f=new Clock();f.setTitle(ll);
   f.setSize(400,150);
  f.setVisible(true);
 
 f.addWindowListener
  (new WindowAdapter()
    {public void windowClosing(WindowEvent e){System.exit(0);}});
   
   
   
 }
 


public Clock()
{setBackground(Color.white);
 if(x==1)
 {ll="alarm clock";
 p1=new Panel();p1.setLayout(new FlowLayout(FlowLayout.CENTER,50,5));
 p1.setBackground(Color.black);
 l1=new Label("alarm off");
 l1.setForeground(Color.green);
 
 l2=new Label();
 t1=new Clock.Clock1();
 t1.start();
 l2.setForeground(Color.white);
 p1.add("West", l1);
 p1.add("East", l2);
 add("North", p1);
 
 p2=new Panel();
 p2.setLayout(new FlowLayout());
 tim=new Choice();
 minut=new Choice();
 int i;
 String str;
 for(i=0;i<24;i++)
 {str=String.valueOf(i);
 tim.add(str);}
 for(i=0;i<60;i++)
 {str=String.valueOf(i);
 minut.add(str);}
 
 alarm=new Button("alarm on");
 alarm.addActionListener(this);
 switcht=new Button("stopwatch");
 switcht.addActionListener(this);
 tim.addItemListener(this);
 minut.addItemListener(this);
 p2.add(tim);
 p2.add(minut);
 
 p2.add(alarm);
 p2.add(switcht);
 add("South", p2);
 }
 else
 { ll="stopwatch";
 l3=new Label("stopwatch", Label.CENTER);
 add("North",l3);
 p3=new Panel();
 p3.setLayout(new FlowLayout());
 reset=new Button("reset");
 reset.addActionListener(this);
 start=new Button("start");
 start.addActionListener(this);
 stop1=new Button("stop");
 stop1.addActionListener(this);
 switcht=new Button("alarm clock");
 switcht.addActionListener(this);
 p3.add(reset);
 p3.add(start);
 p3.add(stop1);
 p3.add(switcht);
 add("South", p3);x=x+2;}
}

public void itemStateChanged(ItemEvent ie)
 {if(ie.getSource()==tim)
  hours=tim.getSelectedIndex();
  else if(ie.getSource()==minut)minutes=minut.getSelectedIndex();
 }
public class Clock2 extends Thread
{int minute1, second1, msecond;
 long current1; 
  public void run()
 {for(;;)
  {try{sleep(50);}
  catch(InterruptedException e){};
   current1=Calendar.getInstance().getTimeInMillis();
  minute1=(int)((current1-current)/60000);
  second1=(int)((current1-current)/1000);second1=(int)(second1%60);
  msecond=(int)((current1-current)%1000);
  msecond=(int)(msecond/10);
  String time2=Clock.type(minute1)
  +":"+Clock.type(second1)
  +":"+Clock.type(msecond);
  l3.setText(time2);}
 }
}
  
public class Clock1 extends Thread
{
 
 int hour, minute, second;
 
 
 
public void run()
{for(;;)
 {try{sleep(50);}
  catch(InterruptedException e){};
  Calendar time=Calendar.getInstance();
 hour=time.get(Calendar.HOUR_OF_DAY);
 minute=time.get(Calendar.MINUTE);
 second=time.get(Calendar.SECOND);
  String time1=Clock.type(hour)
  +":"+Clock.type(minute)
  +":"+Clock.type(second);
  
  l2.setText(time1);
  if(alert==1)
   {if(hours==hour)
    {if(minutes==minute)
      {if(j==1)
       {l1.setBackground(Color.red);j--;}
       else {j++;l1.setBackground(Color.black);}
      }
     else l1.setBackground(Color.black);}}
  else l1.setBackground(Color.black); 
}
}
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==alarm)
    setalarm();
  else if (e.getSource()==switcht)
    setswitcht();
  else if (e.getSource()==reset)y=1; 
  else if(e.getSource()==start)
 {if(y==0){t2=new Clock.Clock2();current=Calendar.getInstance().getTimeInMillis();t2.start();y=2;}
  if(y==1){current=Calendar.getInstance().getTimeInMillis();t2.resume();y=2;}
  if(y==3){t2.resume();y=2;}
 }
  else if(e.getSource()==stop1)
  {t2.suspend();y=3;}
 
}

public static void main(String[] args)
{f= new Clock();
 f.setSize(400,150);
 f.setVisible(true);
 f.addWindowListener
  (new WindowAdapter()
    {public void windowClosing(WindowEvent e){System.exit(0);}});
}
}

⌨️ 快捷键说明

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