📄 fourclocks.java
字号:
import java.awt.*;import javax.swing.*;import java.util.*;import java.text.*;import java.awt.event.*;import java.awt.geom.*;import java.awt.image.*;public class FourClocks extends JFrame implements Runnable, ActionListener{ private volatile Thread timer,timer2,timer3,timer4; //设置4个钟的时针 分针 秒针的坐标 private int clockxs=0, clockys=0, clockxm=0, clockym=0, clockxh=0, clockyh=0, clockxs2=0, clockys2=0, clockxm2=0, clockym2=0, clockxh2=0, clockyh2=0, clockxs3=0, clockys3=0, clockxm3=0, clockym3=0, clockxh3=0, clockyh3=0, clockxs4=0, clockys4=0, clockxm4=0, clockym4=0, clockxh4=0, clockyh4=0, datenum=0; private SimpleDateFormat formatter,formatter2,formatter3,formatter4; // 时间格式 private String lastdate,lastdate2, lastdate3, lastdate4, date = ""; // 在每个钟的下方设置的数字时间 private Font clockFaceFont; // 时钟的字体 private Date currentDate; // 用于输出的日期字体 private Color handColor,handColor1, handColor2,handColor3,handColor4; // 表盘以及指针的颜色 private Color numberColor; // 数字以及秒针的颜色 private int xcenter = 200, ycenter = 150, xcenter2 = 450, ycenter2 = 150, xcenter3 = 200, ycenter3 = 350, xcenter4 = 450, ycenter4 = 350; // 4个表盘的坐标 private JButton buttons1,buttons2,buttons3,buttons4; //4个国家的按钮 public FourClocks() { super("世界各时区时钟"); timer = new Thread(this); //定义线程 formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault()); //世界格式及输出语言 formatter2 = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault()); formatter3 = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault()); formatter4 = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault()); currentDate = new Date(); lastdate = formatter.format(currentDate); lastdate2 = formatter2.format(currentDate); lastdate3 = formatter3.format(currentDate); lastdate4 = formatter4.format(currentDate); TimeZone tz = TimeZone.getDefault();//本地时区 formatter.setTimeZone( tz ); TimeZone tz2 = TimeZone.getTimeZone("America/Los_Angeles");//美国洛杉矶时间 formatter2.setTimeZone( tz2 ); TimeZone tz3 = TimeZone.getTimeZone("Europe/London");//伦敦时间 formatter3.setTimeZone( tz3 ); TimeZone tz4 = TimeZone.getTimeZone("Japan/Tokyo");//东京时间 formatter4.setTimeZone( tz4 ); clockFaceFont = new Font("Serif", Font.PLAIN, 14); handColor = handColor1 = handColor2 = handColor3 = handColor4 = Color.blue;//开始时所有的钟为蓝色表盘 numberColor = Color.darkGray; Container container = getContentPane(); container.setLayout( new FlowLayout() ); Icon image1=new ImageIcon("China.jpg"); Icon image2=new ImageIcon("American.jpg"); Icon image3=new ImageIcon("Jpan.jpg"); Icon image4=new ImageIcon("English.jpg"); buttons1 = new JButton("亚洲/上海"); buttons1.setIcon(image1); buttons1.addActionListener( this ); container.add( buttons1 ); buttons2 = new JButton("北美洲/纽约"); buttons2.setIcon(image2); buttons2.addActionListener( this ); container.add( buttons2 ); buttons3 = new JButton("亚洲/日本"); buttons3.setIcon(image3); buttons3.addActionListener( this ); container.add( buttons3 ); buttons4 = new JButton("欧洲/伦敦"); buttons4.setIcon(image4); buttons4.addActionListener( this ); container.add( buttons4 ); setSize( 700, 500 ); setVisible( true ); } public void actionPerformed( ActionEvent actionEvent ) //按钮监听器 { currentDate = new Date(); Object choice = actionEvent.getSource(); if( choice == buttons1 )//本地时间(北京时间) { handColor1 = Color.red; //设置与按钮对应的时钟的颜色为红色 其他为蓝色 handColor2 = Color.blue; handColor3 = Color.blue; handColor4 = Color.blue; formatter.applyPattern(" yyyy MMM dd EEE HH:mm:ss "); //立刻在程序下方输出时间的具体信息 date = formatter.format(currentDate); datenum = 1; //设置data字符串的内容为按钮对应的国家 不断刷新 repaint(); //按钮按下后立刻重画 } if( choice == buttons2 )//洛杉矶时间 { handColor1 = Color.blue; handColor2 = Color.red; handColor3 = Color.blue; handColor4 = Color.blue; formatter2.applyPattern(" yyyy MMM dd EEE HH:mm:ss "); date = formatter2.format(currentDate); datenum = 2; repaint(); } if( choice == buttons3 )//伦敦时间 { handColor1 = Color.blue; handColor2 = Color.blue; handColor3 = Color.red; handColor4 = Color.blue; formatter3.applyPattern(" yyyy MMM dd EEE HH:mm:ss "); date = formatter3.format(currentDate); datenum = 3; repaint(); } if( choice == buttons4 )//东京时间 { handColor1 = Color.blue; handColor2 = Color.blue; handColor3 = Color.blue; handColor4 = Color.red; formatter4.applyPattern(" yyyy MMM dd EEE HH:mm:ss "); date = formatter4.format(currentDate); datenum = 4; repaint(); } } public void paint(Graphics g) { super.paint( g ); Graphics2D g2d = ( Graphics2D ) g; g.setFont(clockFaceFont);//设置字体 //-------------------第一个钟-------------------- // 钟盘 g.setColor(handColor1); g2d.setStroke(new BasicStroke( 4.0f ) ); g2d.draw( new Arc2D.Double( xcenter-55, ycenter-55,110,110,0,360,Arc2D.OPEN) );//钟盘 并且设置了宽度 g2d.setStroke(new BasicStroke( 2.0f ) ); g.setColor(numberColor); g.drawString("9", xcenter-45, ycenter+3); //钟上的数字刻度 g.drawString("3", xcenter+40, ycenter+3); g.drawString("12", xcenter-5, ycenter-37); g.drawString("6", xcenter-3, ycenter+45); // 指针 g.setColor(numberColor); g.drawString(lastdate, 25, 220); g.drawLine(xcenter, ycenter, clockxs, clockys); g.setColor(handColor); g.drawLine(xcenter, ycenter-1, clockxm, clockym); g.drawLine(xcenter-1, ycenter, clockxm, clockym); g.drawLine(xcenter, ycenter-1, clockxh, clockyh); g.drawLine(xcenter-1, ycenter, clockxh, clockyh); // 1end //-----------------------第二个钟---------------------- g.setColor(handColor2); g2d.setStroke(new BasicStroke( 4.0f ) ); g.drawArc(xcenter2-55, ycenter2-55, 110, 110, 0, 360); g2d.setStroke(new BasicStroke( 2.0f ) ); g.setColor(numberColor); g.drawString("9", xcenter2-45, ycenter2+3); g.drawString("3", xcenter2+40, ycenter2+3); g.drawString("12", xcenter2-5, ycenter2-37); g.drawString("6", xcenter2-3, ycenter2+45); g.setColor(numberColor); g.drawString(lastdate2, 175, 220); g.drawLine(xcenter2, ycenter2, clockxs2, clockys2); g.setColor(handColor); g.drawLine(xcenter2, ycenter2-1, clockxm2, clockym2); g.drawLine(xcenter2-1, ycenter2, clockxm2, clockym2); g.drawLine(xcenter2, ycenter2-1, clockxh2, clockyh2); g.drawLine(xcenter2-1, ycenter2, clockxh2, clockyh2); // 2end //----------------------------第三个钟------------------------- g.setColor(handColor3); g2d.setStroke(new BasicStroke( 4.0f ) ); g.drawArc(xcenter3-55, ycenter3-55, 110, 110, 0, 360); g2d.setStroke(new BasicStroke( 2.0f ) ); g.setColor(numberColor); g.drawString("9", xcenter3-45, ycenter3+3); g.drawString("3", xcenter3+40, ycenter3+3); g.drawString("12", xcenter3-5, ycenter3-37); g.drawString("6", xcenter3-3, ycenter3+45); g.setColor(numberColor); g.drawString(lastdate3, 25, 420); g.drawLine(xcenter3, ycenter3, clockxs3, clockys3); g.setColor(handColor); g.drawLine(xcenter3, ycenter3-1, clockxm3, clockym3); g.drawLine(xcenter3-1, ycenter3, clockxm3, clockym3);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -