| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 701 人关注过本帖
标题:钟表的问题,新人求助,望帮忙
只看楼主 加入收藏
bizika526
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-10-20
收藏
 问题点数:0 回复次数:1 
钟表的问题,新人求助,望帮忙
import java.awt.*;
import javax.swing.*;
import java.util.*;

//定义测试类
//所有变量名冲突的变量名串1了,不过就效果不好~~
 class TimerTest
{
//定义主函数
public static void main(String[] args)
{

//定义JFrame类的一个对象,并创建该对象
MyTimer1 f = new MyTimer1();
//调用MyTimer的show()方法
f.show();

//----------------------------------------------------

//调用类的构造函数
MyTimer myTimer=new MyTimer();

//调用类的显示时间函数
myTimer.displayCurrentTime();

//调用类的设置时间函数
myTimer.setCurrentTime();

//调用类的显示时间函数
myTimer.displayCurrentTime();

//调用类的显示时间函数
myTimer.displayCurrentTime();
System.exit(0);
}
}

//定义MyTimer类
class MyTimer1 extends JFrame
{ static int count=0; //判断是否重定义了时间
//构造函数
public MyTimer1()
{
//定义窗口大小
setSize(320, 200);
//定义窗口标题
setTitle("测试自定义时钟类!");

Container c = getContentPane();
// new ClockCanvas("北京时间", "GMT+8")

c.add(new ClockCanvas("北京时间", "GMT+8"));
}
}

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

class Timer1 extends Thread //类Timer1
{
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 //clockcanvas
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.setOval(100, 5, 120, 120);
g.setColor(Color.orange); //表盘颜色
g.fillOval(100,5,120,120);
g.setColor(Color.black);//指针颜色


//分离时间
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(160, 64, 160 + (int)(20* Math.cos(hourAngle)),
64 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(160, 65, 160 + (int)(20* Math.cos(hourAngle)),
65 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(160, 66, 160 + (int)(20* Math.cos(hourAngle)),
66 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(160, 63, 160 + (int)(20* Math.cos(hourAngle)),
63 + (int)(30 * Math.sin(hourAngle)));
g.drawLine(160, 67, 160 + (int)(20* Math.cos(hourAngle)),
67 + (int)(30 * Math.sin(hourAngle)));

g.drawLine(160, 65, 160 + (int)(32* Math.cos(minuteAngle)),
65 + (int)(40 * Math.sin(minuteAngle)));
g.drawLine(160, 64, 160 + (int)(32* Math.cos(minuteAngle)),
64 + (int)(40 * Math.sin(minuteAngle)));

g.drawLine(160, 65, 160 + (int)(55* Math.cos(secondAngle)),
65 + (int)(45 * Math.sin(secondAngle)));

g.drawString(city, 130, 150);//
}

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

seconds=MyTimer.intHour*60*60+MyTimer.intMinute*60+MyTimer.intSecond;
seconds+=a;//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 displayCurrentTime()
{
JOptionPane.showMessageDialog(null,intHour+":"+intMinute+":"+intSecond);
}


//设定当前时间
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++;

// ClockCanvas.seconds=iHour*60*60+iMinute*60+iSecond;

}

//设定当前时间为系统时间,构造函数调用
public void setCurrentTimeAsSystemTime()
{
//定义Date类的一个对象,用来获取系统时间
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); //Thread 里面方法 目的跟着老大走
}

public void run()
{ try
{
while (!interrupted())
{
sleep(interval);
target.timeElapsed(this);
}
}
catch(InterruptedException e) {}
}
}
class TestChoice extends Frame{
    public static void main(String[] args){
        Frame f=new Frame();
        f.setTitle("我的时钟");
        f.setSize(300,200);
        FlowLayout fl=new FlowLayout();
        f.setLayout(fl);
        Label lb=new Label();
        lb.setAlignment(Label.LEFT);
        lb.setText("请切换钟表表盘颜色");
            f.add(lb);
        Choice ch=new Choice();
            
          ch.add("黑色");
          ch.add("黄色");
          ch.add("红色");
          ch.add("白色");
          ch.select(1);
          
          f.add(ch);
          f.setVisible(true);
    }
}
将切换表盘颜色的单选下拉列表添加到表盘的右上角,怎么添加啊??谢谢
搜索更多相关主题的帖子: 钟表 新人 
2008-03-04 16:20
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
设个位置不就可以 了

可惜不是你,陪我到最后
2008-03-04 17:59
快速回复:钟表的问题,新人求助,望帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012093 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved