java实现的小时钟示例分享
本文导语: 代码如下://package com.clock; import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import ja...
//package com.clock;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import javax.swing.JFrame;
public class Clock extends JFrame implements ActionListener{
private final double RAD=Math.PI/180;
public Clock(){
super("Clock");
setSize(400, 400);
setLocation(400, 200);
setVisible(true);
setResizable(true);
setBackground(Color.white);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
ActionListener drawClock=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
};
new Timer(1000,drawClock).start();
}
@Override
public void actionPerformed(ActionEvent e) {}
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
Insets insert=getInsets();
int h=getSize().height;
g2.setStroke(new BasicStroke(3.0f));
g.drawOval(40,40, h-80,h-80);
int r=200;
int R1=140;
int initRad=60;
for(int i=1;i