可设置Java awt时钟

这个博客展示了如何使用Java AWT库创建一个可设置的时钟应用。通过定义`H2Clock`类,程序在窗口中显示了一个时钟,能够实时更新小时、分钟和秒。用户还可以通过文本框输入时间,并在验证输入格式后更新时钟显示。代码中包含了一个自定义的`Clock`组件,负责绘制指针和刻度,并通过线程不断刷新界面以保持时间的动态更新。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

class H2Clock extends JFrame {
public static Date d = new Date();
private static final long serialVersionUID = 1L;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
TextField tf = new TextField(sdf.format(d), 8);

public H2Clock() {
this.setSize(400, 400);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().setBackground(Color.blue);
this.getContentPane().add(new Clock(), "Center");

tf.setBounds(0, 0, 50, 20);
tf.addKeyListener(new KeyListener() {
@SuppressWarnings("deprecation")
public void keyPressed(KeyEvent e) {
if (tf.getText().length() > 8) {
JOptionPane.showMessageDialog(null, "输入格式有误!");
tf.setText(tf.getText().substring(0, 8));
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println(tf.getText());
try {
Date d0 = sdf.parse(tf.getText());
d.setHours(d0.getHours());
d.setMinutes(d0.getMinutes());
d.setSeconds(d0.getSeconds());
} catch (ParseException e1) {
JOptionPane.showMessageDialog(null, "输入格式有误!");
e1.printStackTrace();
}
}
}

@Override
public void keyTyped(KeyEvent e) {
}

@Override
public void keyReleased(KeyEvent e) {
}
});
this.getContentPane().add(tf, "North");
System.out.println(tf.getText());
this.setTitle("时钟演示");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

public static void main(String args[]) {
JFrame.setDefaultLookAndFeelDecorated(true);
new H2Clock().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

class Clock extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;

// Thread timer;
int oldh = -1, oldm = -1, olds = -1, hq = -1, mq = -1, sq = -1;
final double RAD = Math.PI / 180.0;

Thread th;

public Clock() {
super();
this.setOpaque(false); // 设置透明
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// repaint();

}
};
// new Timer(1000, taskPerformer).start();
th = new Thread(this);
th.start();

}

public void run() {
while (true) {
// System.out.println("HELLO");
repaint();

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO: handle exception
}
}
}

@SuppressWarnings("deprecation")
public void paintComponent(Graphics g) {
int h, m, s, q, x1, y1, x2, y2;
int x0 = this.getWidth() / 2, y0 = this.getHeight() / 2;
int R = (x0 > y0 ? y0 : x0) - 30;

d.setSeconds(d.getSeconds() + 1);

h = d.getHours();
m = d.getMinutes();
s = d.getSeconds();
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x0 - R, y0 - R, 2 * R, 2 * R); // 画表盘
g.setColor(Color.white);
g.drawOval(x0 - R + 3, y0 - R + 3, 2 * R - 7, 2 * R - 7); // 画刻度
for (int i = 0; i < 12; i++) {
y1 = (int) (y0 - Math.cos(i * 30 * RAD) * R * (float) 4 / 5);
x1 = (int) (x0 + Math.sin(i * 30 * RAD) * R * (float) 4 / 5);
y2 = (int) (y0 - Math.cos(i * 30 * RAD) * (R - 3));
x2 = (int) (x0 + Math.sin(i * 30 * RAD) * (R - 3));
g.drawLine(x1, y1, x2, y2);
}
q = h * 30 + m / 2; // 计算时针角度
drawHand(g, x0, y0, 10, R * 4 / 7, q, Color.blue); // 画时针
q = m * 6 + s / 10; // 计算分针角度
drawHand(g, x0, y0, 5, R * 2 / 3, q, Color.yellow); // 画分针
q = s * 6; // 计算秒针角度
drawHand(g, x0, y0, 2, R * 4 / 5, q, Color.red); // 画秒针
g.setColor(Color.black);
g.drawOval(x0 - 2, y0 - 2, 4, 4); // 画轴心
}

void drawHand(Graphics g, int x0, int y0, int w, int L, int q, Color c) {
int x[] = new int[4];
int y[] = new int[4];
x[0] = x0 - (int) (Math.sin(RAD * q) * L / 6);
y[0] = y0 + (int) (Math.cos(RAD * q) * L / 6);
x[1] = x0 + (int) (Math.sin(RAD * (90 - q)) * w);
y[1] = y0 + (int) (Math.cos(RAD * (90 - q)) * w);
x[2] = x0 + (int) (Math.sin(RAD * q) * L);
y[2] = y0 - (int) (Math.cos(RAD * q) * L);
x[3] = x0 - (int) (Math.sin(RAD * (90 - q)) * w);
y[3] = y0 - (int) (Math.cos(RAD * (90 - q)) * w);
g.setColor(c);
g.fillPolygon(x, y, 4);
}
}
}


表盘式时钟,并显import java.awt.*; import java.awt.event.*; import javax.swing.*; import sun.util.calendar.Gregorian; import java.util.Calendar; import java.util.GregorianCalendar; public class Clock extends JFrame implements ActionListener{ int x, y, x0, y0, r, h, olds_x, olds_y, oldm_x, oldm_y, oldh_x, oldh_y, ss,mm,hh,old_m,old_h,ang; final double RAD = Math.PI/180; public Clock(){ super("Java时钟"); setDefaultCloseOperation(3); Image image = getToolkit().getImage("clock.gif"); setIconImage(image); setSize(200,200); setBackground(Color.black); setLocation(300,150); setResizable(false); show(); int delay = 1000; //创建一个监听事件 ActionListener drawClock = new ActionListener(){ public void actionPerformed(ActionEvent evt){ repaint(); } }; //创建一个时间计数器,每一秒触发一次 new Timer(delay,drawClock).start(); } //实现ActionListener接口必须实现的方法 public void actionPerformed(ActionEvent evt){} //绘制图形 public void paint(Graphics g){ Graphics2D g2D = (Graphics2D)g; Insets insets = getInsets(); int L = insets.left/2, T = insets.top/2; h = getSize().height; g.setColor(Color.white); //画圆 g2D.setStroke(new BasicStroke(4.0f)); g.drawOval(L+40, T+40, h-80, h-80); r = h/2 - 40; x0 = 40 + r - 5 + L; y0 = 40 + r - 5 - T; ang = 60; //绘制时钟上的12个字 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; } //获得现在的时间 Calendar now = new GregorianCalendar(); int nowh = now.get(Calendar.HOUR_OF_DAY); int nowm = now.get(Calendar.MINUTE); int nows = now.get(Calendar.SECOND); String st; if(nowh < 10)st = "0"+nowh; else st = ""+nowh; if(nowm<10)st += ":0"+nowm; else st += ":"+nowm; if(nows<10)st += ":0"+nows; else st += ":"+nows; //在窗体上显示时间 g.setColor(Color.pink); g.fillRect(L, T, 50, 28); g.setColor(Color.blue); g.drawString(st,L+2,T+26); //计算时间与度数的关系 ss = 90 - nows*6; mm = 90 - nowm*
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值