谁能帮我改下代码?我实在不会了~

hsdjwb 2009-12-27 11:07:22

改成如图就好
颜色代码没弄明白

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;
}
}
...全文
87 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
crazylaa 2009-12-29
  • 打赏
  • 举报
回复
mark,有空再改。
boboo_2000_0 2009-12-28
  • 打赏
  • 举报
回复
楼主的代码太长了,有空帮你看看。
hsdjwb 2009-12-27
  • 打赏
  • 举报
回复
不管怎么样谢谢LS的了
我会根据你的,再看看的
学习Java中 2009-12-27
  • 打赏
  • 举报
回复

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;
}
}


只会改成这样,字体不会改
相关推荐
发帖
Java

4.9w+

社区成员

Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
帖子事件
创建了帖子
2009-12-27 11:07
社区公告
暂无公告