用JAVA做一个时钟程序

 

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JFrame;
import javax.swing.JPanel;
/**
 * 时间类
 * *
 */
public class Clock extends JFrame {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public Clock() {
        ClockPaint cp = new ClockPaint(20, 20, 70);

        this.add(cp);
        this.setSize(200, 200);
        this.setResizable(false);
        this.setLocation(260, 120);
        this.setTitle("小时钟");
        this.setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

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

class ClockPaint extends JPanel implements Runnable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    int x, y, r;
    int h, m, s;// 小时,分钟,秒
    double rad = Math.PI / 180;

    public ClockPaint(int x, int y, int r) {
        this.x = x;
        this.y = y;
        this.r = r;
        Calendar now = new GregorianCalendar();
        s = now.get(Calendar.SECOND) * 6;// 获得秒转换成度数
        m = now.get(Calendar.MINUTE) * 6;// 获得分钟
        h = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30
                + now.get(Calendar.MINUTE) / 12 * 6;// 获得小时

        Thread t = new Thread(this);
        t.start();
    }

    public void paint(Graphics g) {
        // 清屏
        super.paint(g);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, r * 3, r * 3);
        // 画圆
        g.setColor(Color.WHITE);
        g.drawOval(x, y, r * 2, r * 2);
        // 秒针
        g.setColor(Color.RED);
        int x1 = (int) ((r - 10) * Math.sin(rad * s));
        int y1 = (int) ((r - 10) * Math.cos(rad * s));
        g.drawLine(x + r, y + r, x + r + x1, y + r - y1);
        // 分针
        g.setColor(Color.BLUE);
        x1 = (int) ((r - r / 2.5) * Math.sin(rad * m));
        y1 = (int) ((r - r / 2.5) * Math.cos(rad * m));
        g.drawLine(x + r, y + r, x + r + x1, y + r - y1);
        // 时针
        g.setColor(Color.CYAN);
        x1 = (int) ((r - r / 1.5) * Math.sin(rad * h));
        y1 = (int) ((r - r / 1.5) * Math.cos(rad * h));
        g.drawLine(x + r, y + r, x + r + x1, y + r - y1);
        // 数字
        g.setColor(Color.YELLOW);
        int d = 29;
        for (int i = 1; i <= 12; i++) {
            x1 = (int) ((r - 10) * Math.sin(rad * d));
            y1 = (int) ((r - 10) * Math.cos(rad * d));
            g.drawString(i + "", x + r + x1 - 4, x + r - y1 + 5);
            d += 30;
        }
        // 小点
        d = 0;
        for (int i = 0; i < 60; i++) {
            x1 = (int) ((r - 2) * Math.sin(rad * d));
            y1 = (int) ((r - 2) * Math.cos(rad * d));
            g.drawString(".", x + r + x1 - 1, x + r - y1 + 1);
            d += 6;
        }
        // 显示时间
        //Calendar now1 = new GregorianCalendar();
        //g.drawString(now1.get(Calendar.HOUR_OF_DAY) + ":"
                //+ now1.get(Calendar.MINUTE) + ":" + now1.get(Calendar.SECOND),
                //0, 10);

    }

    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (Exception ex) {
            }
            s += 6;
            if (s >= 360) {
                s = 0;
                m += 6;
                if (m == 72 || m == 144 || m == 216 || m == 288) {
                    h += 6;
                }
                if (m >= 360) {
                    m = 0;
                    h += 6;
                }
                if (h >= 360) {
                    h = 0;
                }
            }
            this.repaint();
        }
    }
}

Java Swing 可以用于编写电子时钟的图形用户界面(GUI)应用程序。以下是一种示例方法,用于创建一个简单的时钟应用程序: 1. 创建一个新的 Java 项目并导入 `javax.swing` 和 `java.awt` 包。 2. 创建一个继承自 `JFrame` 的主窗口类 `ClockFrame`。 3. 在 `ClockFrame` 中创建一个 `JLabel` 组件,用于显示当前时间。例如: ```java JLabel timeLabel = new JLabel(); ``` 4. 创建一个 `Timer` 对象,用于定期更新时间标签。例如: ```java Timer timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { Date currentTime = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); String formattedTime = dateFormat.format(currentTime); timeLabel.setText(formattedTime); } }); timer.start(); ``` 5. 在 `ClockFrame` 的构造函数中设置窗口属性、布局和关闭操作。例如: ```java public ClockFrame() { setTitle("电子时钟"); setSize(200, 100); setLocationRelativeTo(null); setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(timeLabel); } ``` 6. 创建一个 `main` 方法,用于启动程序并显示主窗口。例如: ```java public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new ClockFrame(); frame.setVisible(true); } }); } ``` 以上步骤将创建一个简单的电子时钟应用程序。当程序运行时,窗口将显示当前的小时、分钟和秒钟,并每秒更新一次。你可以根据需要自定义窗口的大小、样式和布局。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值