4.9w+
社区成员
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class TimerTest
{
public static void main(String[] args)
{
JFrame f = new TimerTestFrame();
f.setVisible(true);
}
}
class TimerTestFrame extends JFrame
{
public TimerTestFrame()
{
setSize(450, 300);
setTitle("世界时钟");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel textPanel=new JPanel();
timeText=new JTextField("",5);
timeText.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int[] newtime=new int[6];
String s=timeText.getText().trim();
int hour=Integer.parseInt(s.substring(0,s.indexOf(':')));
int minute=Integer.parseInt(s.substring(s.indexOf(':')+1));
newtime[1]=hour*3600+minute*60;
clock[1].setTime(newtime[1]);
newtime[0]=newtime[1]-16*3600;
newtime[2]=newtime[1]-7*3600;
newtime[3]=newtime[1]-13*3600;
newtime[4]=newtime[1]-6*3600;
newtime[5]=newtime[1]-2*3600-30*60;
for(int i=0;i<6;++i)
{
if(newtime[i]<0) newtime[i]+=24*3600;
clock[i].setTime(newtime[i]);
}
}
});
textPanel.add(timeText);
add(textPanel,BorderLayout.NORTH);
JPanel clockPanel=new JPanel();
clockPanel.setLayout(new GridLayout(2,3));
clock=new ClockCanvas[6];
clock[0]=new ClockCanvas("北京", "GMT+8");
clockPanel.add(clock[0]);
clock[1]=new ClockCanvas("巴黎", "GMT+1");
clockPanel.add(clock[1]);
clock[2]=new ClockCanvas("华盛顿", "GMT-5");
clockPanel.add(clock[2]);
clock[3]=new ClockCanvas("洛杉矶", "GMT-8");
clockPanel.add(clock[3]);
clock[4]=new ClockCanvas("伦敦", "GMT");
clockPanel.add(clock[4]);
clock[5]=new ClockCanvas("芝加哥", "GMT-6");
clockPanel.add(clock[5]);
add(clockPanel,BorderLayout.CENTER);
}
private JTextField timeText;
private ClockCanvas clock[];
}
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) {}
}
}
class ClockCanvas extends JPanel implements TimerListener
{
private int seconds=0,newseconds=0,lastseconds;
private String city;
private GregorianCalendar calendar;
boolean change=false,flag=false;
public ClockCanvas(String c, String tz)
{
city = c;
calendar = new GregorianCalendar(TimeZone.getTimeZone(tz));
Timer t = new Timer(1000, this);
t.start();
setSize(125, 125);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawOval(0, 0, 100, 100);
g.drawString("9",0,50+3);
g.drawString("3",93,50+3);
g.drawString("12",44,10);
g.drawString("6",47,97);
paintLines(g);
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.drawLine(50, 50, 50 + (int)(30* Math.cos(hourAngle)),
50 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(50, 50, 50 + (int)(40 * Math.cos(minuteAngle)),
50 + (int)(40 * Math.sin(minuteAngle)));
g.drawLine(50, 50, 50 + (int)(45 * Math.cos(secondAngle)),
50 + (int)(45 * Math.sin(secondAngle)));
g.drawString(city, 0, 115);
}
public void paintLines(Graphics g) //画秒格
{
int x1, y1, x2, y2;
double sAngle;
for (int i = 0; i < 60; i++)//画一秒的格子
{
sAngle = 2 * Math.PI * (i - 15) / 60;
x1 = 50 + (int) (49 * Math.cos(sAngle));//1个像素宽
y1 = 50 + (int) (49 * Math.sin(sAngle));
x2 = 50 + (int) (50 * Math.cos(sAngle));
y2 = 50 + (int) (50 * Math.sin(sAngle));
g.drawLine(x1, y1, x2, y2);
}
}
public void timeElapsed(Timer t)
{
if(change==true)
{
seconds=newseconds;
lastseconds=seconds;
change=false;
flag=true;
}
else
{
if(flag==false)
{
calendar.setTime(new Date());
seconds = calendar.get(Calendar.HOUR) * 3600
+ calendar.get(Calendar.MINUTE) * 60
+ calendar.get(Calendar.SECOND);
++seconds;
}
else ++seconds;
}
repaint();
}
public void setTime(int newtime)
{
change=true;
newseconds=newtime;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
public class TimerTest
{
public static void main(String[] args)
{
JFrame f = new TimerTestFrame();
f.setVisible(true);
}
}
class TimerTestFrame extends JFrame
{
public TimerTestFrame()
{
setSize(450, 300);
setTitle("世界时钟");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel textPanel=new JPanel();
timeText=new JTextField("",5);
timeText.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int[] newtime=new int[6];
String s=timeText.getText().trim();
int hour=Integer.parseInt(s.substring(0,s.indexOf(':')));
int minute=Integer.parseInt(s.substring(s.indexOf(':')+1));
newtime[1]=hour*3600+minute*60;
clock[1].setTime(newtime[1]);
newtime[0]=newtime[1]-16*3600;
newtime[2]=newtime[1]-7*3600;
newtime[3]=newtime[1]-13*3600;
newtime[4]=newtime[1]-6*3600;
newtime[5]=newtime[1]-2*3600-30*60;
for(int i=0;i<6;++i)
{
if(newtime[i]<0) newtime[i]+=24*3600;
clock[i].setTime(newtime[i]);
}
}
});
textPanel.add(timeText);
add(textPanel,BorderLayout.NORTH);
JPanel clockPanel=new JPanel();
clockPanel.setLayout(new GridLayout(2,3));
clock=new ClockCanvas[6];
clock[0]=new ClockCanvas("北京", "GMT+8");
clock[0].setBackground(Color.BLACK);
clockPanel.add(clock[0]);
clock[1]=new ClockCanvas("巴黎", "GMT+1");
clock[1].setBackground(Color.BLACK);
clockPanel.add(clock[1]);
clock[2]=new ClockCanvas("华盛顿", "GMT-5");
clock[2].setBackground(Color.BLACK);
clockPanel.add(clock[2]);
clock[3]=new ClockCanvas("洛杉矶", "GMT-8");
clock[3].setBackground(Color.BLACK);
clockPanel.add(clock[3]);
clock[4]=new ClockCanvas("伦敦", "GMT");
clock[4].setBackground(Color.BLACK);
clockPanel.add(clock[4]);
clock[5]=new ClockCanvas("芝加哥", "GMT-6");
clock[5].setBackground(Color.BLACK);
clockPanel.add(clock[5]);
add(clockPanel,BorderLayout.CENTER);
}
private JTextField timeText;
private ClockCanvas clock[];
}
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) {}
}
}
class ClockCanvas extends JPanel implements TimerListener
{
private int seconds=0,newseconds=0,lastseconds;
private String city;
private GregorianCalendar calendar;
boolean change=false,flag=false;
public ClockCanvas(String c, String tz)
{
city = c;
calendar = new GregorianCalendar(TimeZone.getTimeZone(tz));
Timer t = new Timer(1000, this);
t.start();
setSize(125, 125);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawOval(0, 0, 100, 100);
g.drawString("9",0,50+3);
g.drawString("3",93,50+3);
g.drawString("12",44,10);
g.drawString("6",47,97);
g.setColor(Color.green );
paintLines(g);
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.drawLine(50, 50, 50 + (int)(30* Math.cos(hourAngle)),
50 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(50, 50, 50 + (int)(40 * Math.cos(minuteAngle)),
50 + (int)(40 * Math.sin(minuteAngle)));
g.drawLine(50, 50, 50 + (int)(45 * Math.cos(secondAngle)),
50 + (int)(45 * Math.sin(secondAngle)));
g.drawString(city, 0, 115);
}
public void paintLines(Graphics g) //画秒格
{
int x1, y1, x2, y2;
double sAngle;
for (int i = 0; i < 60; i++)//画一秒的格子
{
sAngle = 2 * Math.PI * (i - 15) / 60;
x1 = 50 + (int) (49 * Math.cos(sAngle));//1个像素宽
y1 = 50 + (int) (49 * Math.sin(sAngle));
x2 = 50 + (int) (50 * Math.cos(sAngle));
y2 = 50 + (int) (50 * Math.sin(sAngle));
g.drawLine(x1, y1, x2, y2);
}
}
public void timeElapsed(Timer t)
{
if(change==true)
{
seconds=newseconds;
lastseconds=seconds;
change=false;
flag=true;
}
else
{
if(flag==false)
{
calendar.setTime(new Date());
seconds = calendar.get(Calendar.HOUR) * 3600
+ calendar.get(Calendar.MINUTE) * 60
+ calendar.get(Calendar.SECOND);
++seconds;
}
else ++seconds;
}
repaint();
}
public void setTime(int newtime)
{
change=true;
newseconds=newtime;
}
}