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