「java桌面时钟」java电子时钟

博主:adminadmin 2022-11-22 05:41:05 59

今天给各位分享java桌面时钟的知识,其中也会对java电子时钟进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java制作桌面时钟

如果楼主只是想做个简单的电子显示式的时钟,可以有下面的一段代码: Calendar c=new GregorianCalendar(); int hour=c.get(Calendar.HOUR); //得到时间中的小时 int minute=c.get(Calendar.MINUTE); //得到分 int second=c.get(Calendar.SECOND); //得到秒 当然,上面的代码得导入import java.util.*;包 再创建个线程用于自动更新就行了,如果想做成带时针,分针,秒针能走动的那就用canvas 类的画图方法,结合一些数学知识就可以完成图形的了,还有二楼说的data类中的很多方法已经被java6列为过时的了

JAVA画时钟代码

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 ClockPointer extends JFrame{

        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 ClockPointer(){

        super("Java时钟");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Image image = getToolkit().getImage("clock.gif");

        setIconImage(image);

        setSize(400,400);

        setBackground(Color.white);

        //setLocation(300,150);

        this.setLocationRelativeTo(null);

        setResizable(true);

        int delay = 1000;

        //创建一个监听事件

        setVisible(true);

        ActionListener drawClock = new ActionListener(){

            public void actionPerformed(ActionEvent evt){

                repaint();

            }

        };

        //创建一个时间计数器,每一秒触发一次

        new Timer(delay, drawClock).start();

    }

    

    java.text.SimpleDateFormat fmTime = new java.text.SimpleDateFormat("HH:mm:ss");

    //绘制图形

    public void paint(Graphics g){

        super.paint(g);

        g.setFont(null);

        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(2.0f));

        g.setColor(Color.gray);

        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.setColor(Color.black);

            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=fmTime.format(now.getTime());

        //在窗体上显示时间

        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*6;

        hh = 90 - nowh*30 - nowm/2;

        x0 = r+40+L;

        y0 = r+40+T;

        g2D.setStroke(new BasicStroke(1.2f));

        //擦除秒针

        //if(olds_x  0){

        //    g.setColor(getBackground());

        //    // g.setColor(Color.gray);

        //    g.drawLine(x0, y0, olds_x, h-olds_y); // (?)

        //}

        //绘制秒针

        x = (int)(r*0.9*Math.cos(RAD*ss))+x0;

        y = (int)(r*0.9*Math.sin(RAD*ss))+y0-2*T;

        g.setColor(Color.yellow);

        g.drawLine(x0, y0, x, h-y);

        olds_x = x;

        olds_y = y;

        g2D.setStroke(new BasicStroke(2.2f));

        //擦除分针

        //if(old_m!=mm){

        //    g.setColor(getBackground());

        //    g.drawLine(x0,y0,oldm_x,h-oldm_y);

        //}

        //绘制分针

        x = (int)(r*0.7*Math.cos(RAD*mm))+x0;

        y = (int)(r*0.7*Math.sin(RAD*mm))+y0-2*T;

        g.setColor(Color.green);

        g.drawLine(x0,y0,x,h-y);

        oldm_x = x;

        oldm_y = y;

        old_m = mm;

        g2D.setStroke(new BasicStroke(3.2f));

        //擦除时针

        //if(old_h!=hh){

        //    g.setColor(getBackground());

        //    g.drawLine(x0,y0,oldh_x,h-oldh_y);

        //}

        //绘制时针

        x = (int)(r*0.5*Math.cos(RAD*hh))+x0;

        y = (int)(r*0.5*Math.sin(RAD*hh))+y0-2*T;

        g.setColor(Color.red);

        g.drawLine(x0,y0,x,h-y);

        oldh_x = x;

        oldh_y = y;

        old_h = hh;

    }

    public static void main(String[] args){

        new ClockPointer();

    }

}

//整理一下

java时钟

GDI+时钟

import java.awt.*;

import java.awt.geom.*;

import java.util.*;

import javax.swing.*;

public class Clock extends JComponent {

private static final Color INTEGRAL_COLOR = new Color(0, 128, 128);

private int radius;

private Calendar currentTime = Calendar.getInstance();

private double s = 0.03;

public Clock(int radius) {

this.radius = radius;

}

public void setCurrentTime(Date time)

{

this.currentTime.setTime(time);

}

public void setCurrentTime(long millis) {

this.currentTime.setTimeInMillis(millis);

}

public Dimension getPreferredSize() {

Insets insets = getInsets();

int r = (int) (radius == -1 ? 0 : radius * (1 + s)) + 1;

return new Dimension(r * 2 + insets.left + insets.right, r * 2

+ insets.top + insets.bottom);

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

Insets insets = getInsets();

int wid = getWidth() - insets.left - insets.right;

int hei = getHeight() - insets.top - insets.bottom;

int r = (int) ((Math.min(wid, hei)) / 2 / (1 + s));

g2d.translate(insets.left + r * (1 + s), insets.top + r * (1 + s));

g2d.scale(1, -1);

for (int i = 0; i 60; i++) {

int angle = 90 - i * 6;

double pos[] = calcPos(r, angle);

paintMinuteDot(r, g2d, pos[0], pos[1], i % 5 == 0);

}

paintHourPointer(r, g2d);

paintMinutePointer(r, g2d);

paintSecondPointer(r, g2d);

paintCenterPoint(g2d);

g2d.scale(1, -1);

g2d.translate(-insets.left - r * (1 + s), -insets.top - r * (1 + s));

}

private void paintCenterPoint(Graphics2D g2d) {

g2d.setColor(Color.BLUE);

Rectangle2D rect = new Rectangle2D.Double(-2, -2, 4, 4);

g2d.fill(rect);

}

private void paintMinutePointer(int r, Graphics2D g2d) {

int minute = currentTime.get(Calendar.MINUTE);

int second = currentTime.get(Calendar.SECOND);

double angle = 90 - (minute + second / 60.0) * 6;

Shape pointerShape = createPointerShape(r * 0.8, r * 0.04, r * 0.08,

angle);

g2d.setColor(Color.LIGHT_GRAY);

g2d.fill(pointerShape);

g2d.setColor(Color.DARK_GRAY);

g2d.draw(pointerShape);

}

private void paintHourPointer(int r, Graphics2D g2d) {

int hour = currentTime.get(Calendar.HOUR);

int minute = currentTime.get(Calendar.MINUTE);

int second = currentTime.get(Calendar.SECOND);

double angle = 90 - (hour + minute / 60.0 + second / 3600.0) * 30;

Shape pointerShape = createPointerShape(r * 0.6, r * 0.06, r * 0.1,

angle);

g2d.setColor(Color.LIGHT_GRAY);

g2d.fill(pointerShape);

g2d.setColor(Color.DARK_GRAY);

g2d.draw(pointerShape);

}

private Shape createPointerShape(double r1, double r2, double r3,

double angle) {

GeneralPath gp = new GeneralPath();

double[] pos = calcPos(r1, angle);

double[] pos1 = calcPos(r2, angle + 90);

gp.append(new Line2D.Double(pos[0], pos[1], pos1[0], pos1[1]), true);

double[] pos2 = calcPos(r3, angle + 180);

gp.lineTo((float) pos2[0], (float) pos2[1]);

double[] pos3 = calcPos(r2, angle + 270);

gp.lineTo((float) pos3[0], (float) pos3[1]);

gp.closePath();

return gp;

}

private void paintSecondPointer(int r, Graphics2D g2d) {

g2d.setColor(Color.BLACK);

int second = currentTime.get(Calendar.SECOND);

int angle = 90 - second * 6;

double pos[] = calcPos(r * 0.9, angle);

double pos1[] = calcPos(r * 0.2, angle + 180);

Line2D line = new Line2D.Double(pos1[0], pos1[1], pos[0], pos[1]);

g2d.draw(line);

}

private void paintMinuteDot(int r, Graphics2D g2d, double x, double y,

boolean flag) {

g2d.setColor(flag ? Color.RED : Color.BLACK);

if (flag) {

// Rectangle2D rect = new Rectangle2D.Double(

Ellipse2D rect = new Ellipse2D.Double(x - r * s, y - r * s, r * s

* 2, r * s * 2);

g2d.fill(rect);

} else {

// Rectangle2D rect = new Rectangle2D.Double(

Ellipse2D rect = new Ellipse2D.Double(x - r * 0.02, y - r * 0.02,

r * 0.04, r * 0.04);

g2d.fill(rect);

}

}

private double[] calcPos(double r, double angle) {

double radian = Math.toRadians(angle);

double x = r * Math.cos(radian);

double y = r * Math.sin(radian);

return new double[] { x, y };

}

public static void main(String[] args) {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (Exception e) {

e.printStackTrace();

}

final Clock clock = new Clock(50);

clock.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

JFrame f = new JFrame("GDI+时钟");

// f.setBounds(380,200,500,600);

f.this.add(clock, BorderLayout.CENTER);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

new Thread() {

public void run() {

while (true) {

try {

Thread.sleep(1000);

} catch (InterruptedException ex) {

ex.printStackTrace();

}

clock.setCurrentTime(System.currentTimeMillis());

clock.repaint();

}

}

}.start();

}

}

如何用Java中做一个电子时钟

import java.awt.Color;

import java.awt.Font;

import java.util.Date;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class MyTime {

JFrame frame=null;

JLabel label=null;

public MyTime(){

frame=new JFrame("时钟");

label=new JLabel();

label.setFont(new Font("幼圆",1,40));

label.setBackground(Color.BLUE);

frame.add(label);

frame.setSize(200,90);

frame.setLocation(500,300);

frame.setVisible(true);

}

public static void main(String[] args) {

MyTime mt=new MyTime();

new TimeThread(mt).start();

}

}

class TimeThread extends Thread{

private MyTime mt;

public TimeThread(MyTime mt){

this.mt=mt;

}

public void run(){

while(true){

String fullTime=new Date().toString();

String time=fullTime.substring(11, 19);

mt.label.setText(time);

mt.label.repaint();

try {

sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

「java桌面时钟」java电子时钟

Java插入时钟

Clock本身是一个JFrame么?

JFrame是JPanel外层的东西,当然添加不到JPanel里面了。

把Clock换成一个JPanel或者其他的什么组件就可以了

关于java桌面时钟和java电子时钟的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。