java计时器秒表源代码
2个回答
展开全部
import java.awt.*;
public class Clock {
private int hour;
private int minute;
private int second;
public Clock(){
}
public Clock(int hrs,int min,int sec){
hour =hrs % 12;
minute = min;
second = sec;
}
void show (Graphics g,int cx,int cy,int rad){
int hourLenght = (int)(rad * 0.5);//时针的长度
int minuteLenght = (int)(rad * 0.6);//分针的长度
int secondLenght = (int)(rad * 0.9);//秒针的长度
double angle;//角度
//画出钟面
g.drawOval(cx-rad, cy - rad, rad * 2, rad * 2);
//画出时针
angle = (double)(hour*60*60 + minute*60 + second)/43200.0*2.0*Math.PI;
drawNiddle(g,Color.blue, cx, cy, hourLenght,angle);
//画分针
angle = (double)(minute*60 + second)/3600 * 2.0 * Math.PI;
drawNiddle(g,Color.blue, cx, cy, minuteLenght,angle);
// 画秒针
angle = (double)(second)/60*2.0*Math.PI;
drawNiddle(g,Color.blue, cx, cy, secondLenght,angle);
}
private void drawNiddle(Graphics g ,Color c,int x,int y,int len,double angle){
int ex = (int)(x + len * Math.sin(angle));
int ey = (int)(y - len * Math.cos(angle));
g.setColor(c);
g.drawLine(x,y,ex,ey);
}
}
----------------ClockTest1.java---------------------
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class ClockTest1 extends JFrame{
/**
* @param args
*/
private Clock clock ;
private Date timeNow;
public ClockTest1(){
super("时钟");
setSize(400,400);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
timeNow = new Date();
clock = new Clock(timeNow.getHours(),timeNow.getMinutes(),timeNow.getSeconds());
clock.show(g,170,150,100);
try{
//for(int i =0 ; i <10 ; i += 10)
Thread.sleep(1000);
}catch(InterruptedException e){
}
repaint();
}
public static void main(String[] args) {
// TODO 自动生成方法存根
ClockTest1 appication = new ClockTest1();
appication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Clock {
private int hour;
private int minute;
private int second;
public Clock(){
}
public Clock(int hrs,int min,int sec){
hour =hrs % 12;
minute = min;
second = sec;
}
void show (Graphics g,int cx,int cy,int rad){
int hourLenght = (int)(rad * 0.5);//时针的长度
int minuteLenght = (int)(rad * 0.6);//分针的长度
int secondLenght = (int)(rad * 0.9);//秒针的长度
double angle;//角度
//画出钟面
g.drawOval(cx-rad, cy - rad, rad * 2, rad * 2);
//画出时针
angle = (double)(hour*60*60 + minute*60 + second)/43200.0*2.0*Math.PI;
drawNiddle(g,Color.blue, cx, cy, hourLenght,angle);
//画分针
angle = (double)(minute*60 + second)/3600 * 2.0 * Math.PI;
drawNiddle(g,Color.blue, cx, cy, minuteLenght,angle);
// 画秒针
angle = (double)(second)/60*2.0*Math.PI;
drawNiddle(g,Color.blue, cx, cy, secondLenght,angle);
}
private void drawNiddle(Graphics g ,Color c,int x,int y,int len,double angle){
int ex = (int)(x + len * Math.sin(angle));
int ey = (int)(y - len * Math.cos(angle));
g.setColor(c);
g.drawLine(x,y,ex,ey);
}
}
----------------ClockTest1.java---------------------
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class ClockTest1 extends JFrame{
/**
* @param args
*/
private Clock clock ;
private Date timeNow;
public ClockTest1(){
super("时钟");
setSize(400,400);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
timeNow = new Date();
clock = new Clock(timeNow.getHours(),timeNow.getMinutes(),timeNow.getSeconds());
clock.show(g,170,150,100);
try{
//for(int i =0 ; i <10 ; i += 10)
Thread.sleep(1000);
}catch(InterruptedException e){
}
repaint();
}
public static void main(String[] args) {
// TODO 自动生成方法存根
ClockTest1 appication = new ClockTest1();
appication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
展开全部
package wo;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class UserTime extends JFrame implements Runnable
{
JLabel label;
Thread thread;
UserTime time;
String strH;
String strM;
String strS;
/*private int i=0;
public String str;
public void rstart()
{
Timer timer=new Timer();
TimerTask task=new TimerTask()
{
public void run()
{
i++;
str=i+"";
label.setText(str);
}
};
timer.schedule(task, 1000, 1000);
}*/
public UserTime()
{
super("A");
this.setBounds(300, 300, 100, 70);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel=new JPanel();
this.getContentPane().add(panel);
label=new JLabel();
panel.add(label);
this.setVisible(true);
}
public void run()
{
System.out.print("wo");
while(true)
{
Calendar cal=Calendar.getInstance();
int h=cal.get(Calendar.HOUR);
int m=cal.get(Calendar.MINUTE);
int s=cal.get(Calendar.SECOND);
if(h<10)
{
strH="0"+h;
}
else
strH=""+h;
if(m<10)
{
strM="0"+m;
}
else
strM=""+m;
if(s<10)
{
strS="0"+s;
}
else
strS=""+s;
String str=strH+":"+strM+":"+strS+"";
label.setText(str);
try
{
thread.sleep(1000);
}
catch(Exception e)
{}
}
}
public static void main(String[] args)
{
UserTime time=new UserTime();
time.run();
//time.rstart();
}
}
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class UserTime extends JFrame implements Runnable
{
JLabel label;
Thread thread;
UserTime time;
String strH;
String strM;
String strS;
/*private int i=0;
public String str;
public void rstart()
{
Timer timer=new Timer();
TimerTask task=new TimerTask()
{
public void run()
{
i++;
str=i+"";
label.setText(str);
}
};
timer.schedule(task, 1000, 1000);
}*/
public UserTime()
{
super("A");
this.setBounds(300, 300, 100, 70);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel=new JPanel();
this.getContentPane().add(panel);
label=new JLabel();
panel.add(label);
this.setVisible(true);
}
public void run()
{
System.out.print("wo");
while(true)
{
Calendar cal=Calendar.getInstance();
int h=cal.get(Calendar.HOUR);
int m=cal.get(Calendar.MINUTE);
int s=cal.get(Calendar.SECOND);
if(h<10)
{
strH="0"+h;
}
else
strH=""+h;
if(m<10)
{
strM="0"+m;
}
else
strM=""+m;
if(s<10)
{
strS="0"+s;
}
else
strS=""+s;
String str=strH+":"+strM+":"+strS+"";
label.setText(str);
try
{
thread.sleep(1000);
}
catch(Exception e)
{}
}
}
public static void main(String[] args)
{
UserTime time=new UserTime();
time.run();
//time.rstart();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询