使用Java Applet开发一个模拟时钟

要求能正确显示当前时间和日期各位有才的人。谢谢你们了。。。。... 要求能正确显示当前时间和日期
各位有才的人。谢谢你们了。。。。
展开
 我来答
摩已人s
推荐于2016-10-31
知道答主
回答量:7
采纳率:0%
帮助的人:0
展开全部
这里有书上源码一篇,可以参考。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Calendar;
import javax.swing.*;

public class Clock extends JPanel implements ActionListener
{
// 创建时钟的外形
protected static Ellipse2D face = new Ellipse2D.Float(3, 3, 94, 94);
// 创建时钟的标记
protected static GeneralPath tick = new GeneralPath();
static
{
tick.moveTo(100, 100);
tick.moveTo(49, 0);
tick.lineTo(51, 0);
tick.lineTo(51, 6);
tick.lineTo(49, 6);
tick.lineTo(49, 0);
}
// 创建时针
protected static GeneralPath hourHand = new GeneralPath();
static
{
hourHand.moveTo(50, 15);
hourHand.lineTo(53, 50);
hourHand.lineTo(50, 53);
hourHand.lineTo(47, 50);
hourHand.lineTo(50, 15);
}
// 创建分针
protected static GeneralPath minuteHand = new GeneralPath();
static
{
minuteHand.moveTo(50, 2);
minuteHand.lineTo(53, 50);
minuteHand.lineTo(50, 58);
minuteHand.lineTo(47, 50);
minuteHand.lineTo(50, 2);
}
// 创建秒针
protected static GeneralPath secondHand = new GeneralPath();
static
{
secondHand.moveTo(49, 5);
secondHand.lineTo(51, 5);
secondHand.lineTo(51, 62);
secondHand.lineTo(49, 62);
secondHand.lineTo(49, 5);
}
// 设置时钟的颜色
protected static Color faceColor = new Color(220, 220, 220);
protected static Color hourColor = Color.red.darker();
protected static Color minuteColor = Color.blue.darker();
protected static Color secondColor = new Color(180, 180, 0);
protected static Color pinColor = Color.gray.brighter();
// 创建时钟的枢纽
protected Ellipse2D pivot = new Ellipse2D.Float(47, 47, 6, 6);
protected Ellipse2D centerPin = new Ellipse2D.Float(49, 49, 2, 2);

// 创建绕时钟枢纽转的变换
protected AffineTransform hourTransform =
AffineTransform.getRotateInstance(0, 50, 50);
protected AffineTransform minuteTransform =
AffineTransform.getRotateInstance(0, 50, 50);
protected AffineTransform secondTransform =
AffineTransform.getRotateInstance(0, 50, 50);
// 创建每秒触发一次的Timer
protected Timer timer = new Timer(1000, this);
protected Calendar calendar = Calendar.getInstance();

public Clock()
{
setPreferredSize(new Dimension(100, 100));
}
// 当plane加入container中时发生
public void addNotify()
{
super.addNotify();
timer.start();
}
// 当plane从container中移处时发生
public void removeNotify()
{
timer.stop();
super.removeNotify();
}

public void actionPerformed(ActionEvent event)
{
// 更新calender的时间
this.calendar.setTime(new java.util.Date());
int hours = this.calendar.get(Calendar.HOUR);
int minutes = this.calendar.get(Calendar.MINUTE);
int seconds = this.calendar.get(Calendar.SECOND);

//设置变换,使得时针、分针、秒针各自按绕枢纽旋转一定的角度
hourTransform.setToRotation(((double) hours) *
(Math.PI / 6.0), 50, 50);
minuteTransform.setToRotation(((double) minutes) *
(Math.PI / 30.0), 50, 50);
secondTransform.setToRotation(((double) seconds) *
(Math.PI / 30.0), 50, 50);
repaint();
}

public void paint(Graphics g)
{
super.paint(g);

// 得到图形上下文和抗锯齿处理
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(faceColor);
g2.fill(face);
g2.setPaint(Color.black);
g2.draw(face);

// 产生钟面上12个滴答位置
for (double p = 0.0; p < 12.0; p += 1.0)
{
//利用变换画出同心的滴答的标线
g2.fill(tick.createTransformedShape(
AffineTransform.getRotateInstance((Math.PI / 6.0) * p,
50, 50)));
}
g2.setPaint(hourColor);
g2.fill(hourHand.createTransformedShape(hourTransform));
g2.setPaint(minuteColor);
g2.fill(minuteHand.createTransformedShape(minuteTransform));
g2.setPaint(secondColor);
g2.fill(secondHand.createTransformedShape(secondTransform));
g2.fill(pivot);
g2.setPaint(pinColor);
g2.fill(centerPin);
}

public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setLocation(700, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Clock());
frame.pack();
frame.show();
}
}
capuchin
2007-12-04 · TA获得超过678个赞
知道小有建树答主
回答量:199
采纳率:0%
帮助的人:113万
展开全部
import java.applet.Applet;
import java.awt.BorderLayout;
import java.util.Date;

import javax.swing.JLabel;

/**
*
* @author capuchin
* @date Dec 4, 2007
*
*/
public class FirstApplet extends Applet {

static final long serialVersionUID = 1L;
String time = null;
JLabel lbshow = null;

public void init() {
lbshow = new JLabel();
time = "Now Time:";
showTime t = new showTime();
t.start();
setLayout(new BorderLayout());
add(lbshow, BorderLayout.CENTER);
}

private class showTime extends Thread {
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
time = "Now Time: " + new Date().toLocaleString();
lbshow.setText(time);
}
}
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式