⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock1.java

📁 这是一个在java环境下实现的日历应用程序
💻 JAVA
字号:
//package clock;

import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Clock1 extends JPanel implements Runnable{
    public Clock1()
    {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try
        {
            jbInit();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    Thread thread1;//定义线程对象
    Color handColor;//定义表示时针、分针和秒针颜色的变量
    Color numberColor;//定义表示时钟数字颜色的变量

    JLabel jLabel1 = new JLabel();
    public void init()
    {
        int x,y;
        handColor = Color.blue;
        numberColor = Color.darkGray;
        setBackground(Color.white);
    }
    //paint is the main part of the program
    public void paint(Graphics g)
    {
        int xh,yh,xm,ym,xs,ys,s = 0,m = 10,h=0,xpoint,ypoint;
        int day,month,year,weekday;
        Calendar c1 = Calendar.getInstance();
        //或的当前时间
        s = c1.get(Calendar.SECOND);
        m = c1.get(Calendar.MINUTE);
        h = c1.get(Calendar.HOUR_OF_DAY);
        //获得当前日期
        day = c1.get(Calendar.DATE);
        month = c1.get(Calendar.MONTH) + 1;
        year = c1.get(Calendar.YEAR);
        weekday = c1.get(Calendar.DAY_OF_WEEK)-1;
        //在标签控件上显示日期和时间
        jLabel1.setText("    "+year+"年"+month+"月"+day+"日"+"    "+"星期"+weekday+"    "+h+":"+m+":"+s);
        xpoint = 130;
        ypoint = 100;
        //计算时针、分针和秒针的位置
        xs = (int)(Math.cos(s * 3.14f/30 -3.14f/2) * 45 + xpoint);
        ys = (int)(Math.sin(s * 3.14f/30 -3.14f/2) * 45 + ypoint);
        xm = (int)(Math.cos(m * 3.14f/30 -3.14f/2) * 40 + xpoint);
        ym = (int)(Math.sin(m * 3.14f/30 -3.14f/2) * 40 + ypoint);
        xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 +xpoint);
        yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 +ypoint);
        //绘制时钟背景
        g.setColor(handColor);
        g.clearRect(0,0,260,200);
        g.drawOval(xpoint/2+10,ypoint/2-5,110,110);
        g.setColor(numberColor);
        g.drawString("9",xpoint-45,ypoint+3);
        g.drawString("3",xpoint+40,ypoint+3);
        g.drawString("12",xpoint-5,ypoint-37);
        g.drawString("6",xpoint-3,ypoint+45);
        g.setColor(getBackground());

        g.setColor(numberColor);
        g.drawString("",5,125);
        //绘制时针、分针和秒针
        g.drawLine(xpoint,ypoint,xs,ys);
        g.setColor(handColor);
        g.drawLine(xpoint,ypoint,xm,ym);
        g.drawLine(xpoint,ypoint,xh,yh);
    }

    //定义线程的run方法
    public void run()
    {
        Thread me = Thread.currentThread();
        while(thread1 == me)
        {
            try
            {
                Thread.currentThread().sleep(100);
            }
            catch(InterruptedException e)
            {

            }
            repaint();
        }
    }
    //开始执行线程
    public void start()
    {
        thread1 = new Thread(this);
        thread1.start();
    }
    //停止线程
    public void stop()
    {
        thread1 = null;
    }
    public void update(Graphics g)
    {
        paint(g);
    }
    private void jbInit() throws Exception
    {
        add(jLabel1,BorderLayout.SOUTH);
    }
   // protected void processWindowEvent(WindowEvent e)
   // {
       // super.processWindowEvent(e);
     //   if(e.getID() == WindowEvent.WINDOW_CLOSING)
       // {
        //    System.exit(0);
       // }

    //}

    //public static void main(String[] args) {
    //    Clock1 clock1 = new Clock1();
        //clock1.init();
       // clock1.start();
       // clock1.setSize(280,230);
       // clock1.setResizable(false);
       // clock1.setVisible(true);

    //}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -