导航:首页 > 编程语言 > java时钟代码看不懂

java时钟代码看不懂

发布时间:2025-01-09 15:17:24

A. 用java编写小时钟

貌似这个网上有JS插件的

B. 用Java编写一个时钟都需要用到什么

文本域和监听事件,下面是一个简单的时钟显示,您可以参考一下是不是您想要的类型
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class Css extends Frame implements Runnable{
TextField tf = new TextField();//定义文本框
public Css(){//自定义构造函数
setTitle("电子表");//实例化窗体对象,设定显示标题为电子表
setSize(200,200);//设定窗体大小
add(tf);//向容器中添加组件tf
addWindowListener(new WindowAdapter(){//创建匿名对象类,添加监听事件
public void windowClosing(WindowEvent event){
System.exit(0);//让监听事件正常退出
}
});
setVisible(true);//让组件显示出来
}

public static void main(String[] args){
Css a = new Css();//创建c类的实例对象a
Thread t = new Thread(a);//创建并实例化线程对象t
t.start();//启动线程
}

@SuppressWarnings("deprecation")
public void run(){//复写run()方法
while(true){//只要没有点击退出窗体就把当前系统时间显示在文本域中
try{
tf.setText(new java.util.Date().toLocaleString());
Thread.sleep(1000);//每秒刷新一下当前显示时间
}catch(Exception ex){

}
}
}
}

C. 怎么用JAVA编程实现实时动态运行的模拟时钟

import java.awt.*;
import java.applet.Applet;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ClockApplet extends Applet implements Runnable //Applet支持线程
{
private Thread athread; //线程
private SimpleDateFormat sdateformat; //日期格式

public void init()
{

this.setBackground(Color.white);//背景颜色设为白色
this.athread = null;
}
public void paint(Graphics g)
{
this.sdateformat = new SimpleDateFormat("hh时mm分ss秒");
g.drawString(this.sdateformat.format(new Date()),25,131);

Calendar rightnow = Calendar.getInstance();
int second = rightnow.get(Calendar.SECOND);
int minute = rightnow.get(Calendar.MINUTE);
int hour = rightnow.get(Calendar.HOUR);

//半径
int R_H = 20,R_M = 4,R_S = 4;
//时针的坐标
//x ====(9-3)[0-6] (3-9)[6-0]
//y ====(12-6)[0-6] (6-12)[6-0]
int H_x ;
int H_y;
//x
if(hour == 0)
{
hour = 12;
}
if( hour >= 3 && hour <= 9 )
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
if(hour > 9)
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
H_x = R_H*Math.abs(hour+3);
}
}
//y
if( hour >= 6 && hour <= 12 )
{
H_y = R_H*Math.abs(hour - 12);
}
else
{
H_y = R_H*hour;
}

//分针的坐标
int M_x;
int M_y;

if(minute == 0)
{
minute = 60;
}
if( minute >= 15 && minute <= 45 )
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
if(minute > 45)
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
M_x = R_M*Math.abs(minute+15);
}
}
//y
if( minute >= 30 && minute < 60 )
{
M_y = R_M*Math.abs(minute - 60);
}
else
{
M_y = R_M*minute;
}

//秒针的坐标
int S_x;
int S_y;

if(second == 0)
{
second = 60;
}
if( second >= 15 && second <= 45 )
{
S_x = R_S*Math.abs(second - 45);
}
else
{
if(second > 45)
{
S_x = R_S*Math.abs(second - 45);
}
else
{
S_x = R_S*Math.abs(second+15);
}
}
//y
if( second >= 30 && second <= 60 )
{
S_y = R_S*Math.abs(second - 60);
}
else
{
S_y = R_S*second;
}

// g.drawString(String.valueOf(second),25,50);
// g.drawString(String.valueOf(minute),25,60);
// g.drawString(String.valueOf(hour),25,70);
// g.drawString(String.valueOf(H_x),25,80);
// g.drawString(String.valueOf(H_y),25,90);
g.drawOval(0,0,120,120);//距离相差10像素
g.setColor(Color.darkGray);
g.drawString("9",5,65);
g.drawString("3",110,65);
g.drawString("12",55,15);
g.drawString("6",55,115);

g.drawString("1",80,20);
g.drawString("2",100,40);
g.drawString("4",100,90);
g.drawString("5",80,110);

g.drawString("7",30,110);
g.drawString("8",10,90);
g.drawString("10",10,40);
g.drawString("11",30,20);

g.setColor(Color.red);
g.drawLine(60,60,H_x,H_y);//前一个点表示起点,另一个表示终点
g.setColor(Color.blue);
g.drawLine(60,60,M_x,M_y);
g.setColor(Color.yellow);
g.drawLine(60,60,S_x,S_y);

}
public void start()
{
if(athread == null)
{
athread = new Thread(this);
athread.start();
}
}
public void stop()
{
if(athread != null)
{
athread.interrupt();
athread = null;
}
}
public void run()
{
while(athread != null)
{
repaint();
try
{
athread.sleep(1000);
}
catch(InterruptedException e)
{

}
}
}

}

D. JAVA 编写时钟程序

分太少了啊
给你一个只实现了时钟的面板。
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;

public class Clock extends JFrame implements ActionListener
{
private static final long serialVersionUID = 6790815213225162093L;
Timer timer;
int x,y,old_X,old_Y, r,x0,y0,w,h,ang;
int sdo,mdo,hdo,old_M,old_H;
TimeZone tz =TimeZone.getTimeZone("JST");
final double RAD=Math.PI/180.0;

public Clock()
{
super("时钟");
setSize(300,300);
setBackground(new Color(0,0,192));
setResizable(false);
Dimension scr=Toolkit.getDefaultToolkit().getScreenSize();
Dimension fra=this.getSize();
if(fra.width>scr.width)
{
fra.width=scr.width;
}
if(fra.height>scr.height)
{
fra.height=scr.height;
}
this.setLocation((scr.width-fra.width)/2,(scr.height-fra.height)/2);
setVisible(true);
int delay = 1000;
ActionListener taskPerformer = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
repaint();
}
};
new Timer(delay, taskPerformer).start();
}

public void actionPerformed(ActionEvent e)
{
timer.restart();
}

public void paint( Graphics g )
{
Insets insets = getInsets();
int L0 = (insets.left)/2, T0 = (insets.top)/2;
int hh,mm,ss;
String st;
h=getSize().height;
g.setColor(Color.white);
g.drawOval(L0+30,T0+30,h-60,h-60);
g.drawOval(L0+32,T0+32,h-64,h-64);
r=h/2-30;
x0=30+r-5+L0;
y0=30+r-5-T0;
ang=60;
for (int i=1; i<=12; i++)
{
x=(int)((r+10)*Math.cos(RAD*ang)+x0);
y=(int)((r+10)*Math.sin(RAD*ang)+y0);
g.drawString(""+i,x,h-y);
ang-=30;
}
x0=30+r+L0; y0=30+r+T0;
Calendar now=Calendar.getInstance();
hh=now.get(Calendar.HOUR_OF_DAY);
mm=now.get(Calendar.MINUTE);
ss=now.get(Calendar.SECOND);
g.setColor(Color.pink);
g.fillRect(L0,T0,60,28);
g.setColor(Color.blue);
if (hh < 10) st="0"+hh; else st=""+hh;
if (mm < 10) st=st+":0"+mm; else st=st+":"+mm;
if (ss < 10) st=st+":0"+ss; else st=st+":"+ss;
g.drawString(st,L0,T0+25);
sdo=90-ss*6;
mdo=90-mm*6;
hdo=90-hh*30-mm/2;
if (old_X > 0)
{
g.setColor(getBackground());
g.drawLine(x0,y0,old_X,(h-old_Y));
} else
{
old_M=mdo;
old_H=hdo;
}
g.setColor(Color.yellow);
x=(int)((r-8)*Math.cos(RAD*sdo)+x0);
y=(int)((r-8)*Math.sin(RAD*sdo)+y0)-2*T0;
g.drawLine(x0,y0,x,(h-y));

old_X=x;
old_Y=y;
if (mdo != old_M)
{
line(g,old_M,(int)(r*0.7),getBackground());
old_M=mdo;
}
if (hdo != old_H)
{
line(g,old_H,(int)(r*0.5),getBackground());
old_H=hdo;
}
line(g,mdo,(int)(r*0.7),Color.green);
line(g,hdo,(int)(r*0.5),Color.red);
}

public void line(Graphics g, int t, int n, Color c)
{
int [] xp = new int[4];
int [] yp = new int[4];
xp[0]=x0;
yp[0]=y0;
xp[1]= (int)((n-10)*Math.cos(RAD*(t-4))+x0);
yp[1]=h-(int)((n-10)*Math.sin(RAD*(t-4))+y0);
xp[2]= (int)( n *Math.cos(RAD* t )+x0);
yp[2]=h-(int)( n *Math.sin(RAD* t )+y0);
xp[3]= (int)((n-10)*Math.cos(RAD*(t+4))+x0);
yp[3]=h-(int)((n-10)*Math.sin(RAD*(t+4))+y0);
g.setColor(c);
g.fillPolygon(xp,yp,4);
}

public static void main(String args[])
{
new Clock();
}
}

E. 需要用java编写日历或时钟源代码

时钟----------------
import java.awt.*;
import java.awt.event.*;
import java.util.*;//这两个包你没有导入 至少在你贴进来的代码中没有导入
import java.text.SimpleDateFormat;

public class test extends Frame implements Runnable
{
private Label Labelshow=new Label();
private Panel pan1=new Panel();

public test()
{
super("time");
setup();
setResizable(false); //设置此图形界面是不可以改变大小的
setBounds(400, 200, 200, 400);
add(pan1);//修改1 你没有添加Panel界面会什么都不显示的
pack();
setVisible(true);
}

public void setup()
{
pan1.add(Labelshow);
Thread thread1=new Thread(this);//修改2 Panel没有实现Runnable接口 不能用做线程启动的
thread1.start();
}

public void run()
{
while(true)
{
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Labelshow.setText(sdf.format(new Date()));
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
Labelshow.setText("出错错误,请重启程序");
}
}
}

public static void main(String[] args)
{
test te=new test();
}
}

阅读全文

与java时钟代码看不懂相关的资料

热点内容
苹果北京维修点地址 浏览:928
app原型图是什么东西 浏览:237
ug直接用cad文件建模 浏览:272
苹果电脑打开mp4文件 浏览:337
数据线一直发烫怎么回事 浏览:830
IBM代码5 浏览:279
编程时如何让输出结果之间有空格 浏览:112
怎么复制网上文件 浏览:675
苹果怎么关掉网站禁止访问 浏览:478
win10适合机械硬盘吗 浏览:586
qq头像欧美男霸气头像 浏览:368
java批量修改数据返回值 浏览:517
wp81创建文件夹 浏览:968
iphone高科技软件 浏览:654
手机端怎么看网页代码 浏览:925
什么软件能把手机变成苹果版本的 浏览:841
ps精修瓶子视频教程 浏览:481
win10惠普recovery盘 浏览:172
电脑网络问题是怎么回事 浏览:193
微信刷卡支付接口开发 浏览:443

友情链接