问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

用Java编写小时钟

发布网友 发布时间:2022-04-29 04:24

我来回答

2个回答

热心网友 时间:2023-10-11 08:04

貌似这个网上有JS插件的

热心网友 时间:2023-10-11 08:04

貌似这个网上有JS插件的

热心网友 时间:2023-10-11 08:04

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* This is the main Function of the program.
*/
public class Clock{
public static void main(String []args){
ClockFrame frame = new ClockFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
* This class is used to define the main frame of the clock
*/
class ClockFrame extends JFrame{
//constructor function
public ClockFrame(){
setTitle("小时钟");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLocation(DEFAULT_LOC_WIDTH, DEFAULT_LOC_HEIGHT);
ClockPanel panel = new ClockPanel();
add(panel);
}
//variables of the frame
private int DEFAULT_LOC_WIDTH = 300;
private int DEFAULT_LOC_HEIGHT = 300;
private int DEFAULT_WIDTH = 330;
private int DEFAULT_HEIGHT = 330;
}
/**
* This class is used to defind the main panel of the clock
*/
class ClockPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//get the time of the system
GregorianCalendar calendar = new GregorianCalendar();
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
//draw the clock face
Ellipse2D clockFace = new Ellipse2D.Double();
clockFace.setFrameFromCenter(CENTER_X, CENTER_Y, CENTER_X+RADIUS, CENTER_Y+RADIUS);
g2.setColor(Color.BLUE);
g2.draw(clockFace);
//draw the clock center
Ellipse2D clockCenter = new Ellipse2D.Double();
clockCenter.setFrameFromCenter(CENTER_X, CENTER_Y, CENTER_X+INNER_RADIUS, CENTER_Y+INNER_RADIUS);
g2.setColor(Color.RED);
g2.fill(clockCenter);
//help to get the exact position of the lines
double lenX, lenY, posX, posY;
//draw the clock second line
Line2D clockSecond = new Line2D.Double();
double secondTime = (double) calendar.get(Calendar.SECOND);
lenX = SECOND_LEN*Math.sin(2*Math.PI*secondTime/60.0);
lenY = SECOND_LEN*Math.cos(2*Math.PI*secondTime/60.0);
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockSecond.setLine(CENTER_X, CENTER_Y, posX, posY);
g2.setColor(Color.PINK);
g2.draw(clockSecond);
//draw the clock minute line
Line2D clockMinute = new Line2D.Double();
double minuteTime = (double) calendar.get(Calendar.MINUTE);
lenX = MINUTE_LEN*Math.sin(2*Math.PI*(secondTime+60*minuteTime)/3600.0);
lenY = MINUTE_LEN*Math.cos(2*Math.PI*(secondTime+60*minuteTime)/3600.0);
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockMinute.setLine(CENTER_X, CENTER_Y, posX, posY);
g2.setColor(Color.GREEN);
g2.draw(clockMinute);
//draw the clock hour line
Line2D clockHour = new Line2D.Double();
double hourTime = (double) calendar.get(Calendar.HOUR);
lenX = HOUR_LEN*Math.sin(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));
lenY = HOUR_LEN*Math.cos(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockHour.setLine(CENTER_X, CENTER_Y, posX, posY);
g2.setColor(Color.BLUE);
g2.draw(clockHour);
int delay = 1000;
// actionListener
ActionListener drawClock;
drawClock=new ActionListener(){
public void actionPerformed(ActionEvent evt){
repaint();
}
};
//create timer
new Timer(delay, drawClock).start();
}
//variables of the panel
private int HOUR_LEN = 50;
private int MINUTE_LEN = 70;
private int SECOND_LEN = 90;
private int RADIUS = 100;
private int INNER_RADIUS = 2;
private int CENTER_X = 150;
private int CENTER_Y = 150;
}

热心网友 时间:2023-10-11 08:04

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* This is the main Function of the program.
*/
public class Clock{
public static void main(String []args){
ClockFrame frame = new ClockFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
* This class is used to define the main frame of the clock
*/
class ClockFrame extends JFrame{
//constructor function
public ClockFrame(){
setTitle("小时钟");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLocation(DEFAULT_LOC_WIDTH, DEFAULT_LOC_HEIGHT);
ClockPanel panel = new ClockPanel();
add(panel);
}
//variables of the frame
private int DEFAULT_LOC_WIDTH = 300;
private int DEFAULT_LOC_HEIGHT = 300;
private int DEFAULT_WIDTH = 330;
private int DEFAULT_HEIGHT = 330;
}
/**
* This class is used to defind the main panel of the clock
*/
class ClockPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//get the time of the system
GregorianCalendar calendar = new GregorianCalendar();
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
//draw the clock face
Ellipse2D clockFace = new Ellipse2D.Double();
clockFace.setFrameFromCenter(CENTER_X, CENTER_Y, CENTER_X+RADIUS, CENTER_Y+RADIUS);
g2.setColor(Color.BLUE);
g2.draw(clockFace);
//draw the clock center
Ellipse2D clockCenter = new Ellipse2D.Double();
clockCenter.setFrameFromCenter(CENTER_X, CENTER_Y, CENTER_X+INNER_RADIUS, CENTER_Y+INNER_RADIUS);
g2.setColor(Color.RED);
g2.fill(clockCenter);
//help to get the exact position of the lines
double lenX, lenY, posX, posY;
//draw the clock second line
Line2D clockSecond = new Line2D.Double();
double secondTime = (double) calendar.get(Calendar.SECOND);
lenX = SECOND_LEN*Math.sin(2*Math.PI*secondTime/60.0);
lenY = SECOND_LEN*Math.cos(2*Math.PI*secondTime/60.0);
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockSecond.setLine(CENTER_X, CENTER_Y, posX, posY);
g2.setColor(Color.PINK);
g2.draw(clockSecond);
//draw the clock minute line
Line2D clockMinute = new Line2D.Double();
double minuteTime = (double) calendar.get(Calendar.MINUTE);
lenX = MINUTE_LEN*Math.sin(2*Math.PI*(secondTime+60*minuteTime)/3600.0);
lenY = MINUTE_LEN*Math.cos(2*Math.PI*(secondTime+60*minuteTime)/3600.0);
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockMinute.setLine(CENTER_X, CENTER_Y, posX, posY);
g2.setColor(Color.GREEN);
g2.draw(clockMinute);
//draw the clock hour line
Line2D clockHour = new Line2D.Double();
double hourTime = (double) calendar.get(Calendar.HOUR);
lenX = HOUR_LEN*Math.sin(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));
lenY = HOUR_LEN*Math.cos(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockHour.setLine(CENTER_X, CENTER_Y, posX, posY);
g2.setColor(Color.BLUE);
g2.draw(clockHour);
int delay = 1000;
// actionListener
ActionListener drawClock;
drawClock=new ActionListener(){
public void actionPerformed(ActionEvent evt){
repaint();
}
};
//create timer
new Timer(delay, drawClock).start();
}
//variables of the panel
private int HOUR_LEN = 50;
private int MINUTE_LEN = 70;
private int SECOND_LEN = 90;
private int RADIUS = 100;
private int INNER_RADIUS = 2;
private int CENTER_X = 150;
private int CENTER_Y = 150;
}

热心网友 时间:2023-10-11 08:04

貌似这个网上有JS插件的

热心网友 时间:2023-10-11 08:04

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* This is the main Function of the program.
*/
public class Clock{
public static void main(String []args){
ClockFrame frame = new ClockFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
* This class is used to define the main frame of the clock
*/
class ClockFrame extends JFrame{
//constructor function
public ClockFrame(){
setTitle("小时钟");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLocation(DEFAULT_LOC_WIDTH, DEFAULT_LOC_HEIGHT);
ClockPanel panel = new ClockPanel();
add(panel);
}
//variables of the frame
private int DEFAULT_LOC_WIDTH = 300;
private int DEFAULT_LOC_HEIGHT = 300;
private int DEFAULT_WIDTH = 330;
private int DEFAULT_HEIGHT = 330;
}
/**
* This class is used to defind the main panel of the clock
*/
class ClockPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//get the time of the system
GregorianCalendar calendar = new GregorianCalendar();
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
//draw the clock face
Ellipse2D clockFace = new Ellipse2D.Double();
clockFace.setFrameFromCenter(CENTER_X, CENTER_Y, CENTER_X+RADIUS, CENTER_Y+RADIUS);
g2.setColor(Color.BLUE);
g2.draw(clockFace);
//draw the clock center
Ellipse2D clockCenter = new Ellipse2D.Double();
clockCenter.setFrameFromCenter(CENTER_X, CENTER_Y, CENTER_X+INNER_RADIUS, CENTER_Y+INNER_RADIUS);
g2.setColor(Color.RED);
g2.fill(clockCenter);
//help to get the exact position of the lines
double lenX, lenY, posX, posY;
//draw the clock second line
Line2D clockSecond = new Line2D.Double();
double secondTime = (double) calendar.get(Calendar.SECOND);
lenX = SECOND_LEN*Math.sin(2*Math.PI*secondTime/60.0);
lenY = SECOND_LEN*Math.cos(2*Math.PI*secondTime/60.0);
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockSecond.setLine(CENTER_X, CENTER_Y, posX, posY);
g2.setColor(Color.PINK);
g2.draw(clockSecond);
//draw the clock minute line
Line2D clockMinute = new Line2D.Double();
double minuteTime = (double) calendar.get(Calendar.MINUTE);
lenX = MINUTE_LEN*Math.sin(2*Math.PI*(secondTime+60*minuteTime)/3600.0);
lenY = MINUTE_LEN*Math.cos(2*Math.PI*(secondTime+60*minuteTime)/3600.0);
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockMinute.setLine(CENTER_X, CENTER_Y, posX, posY);
g2.setColor(Color.GREEN);
g2.draw(clockMinute);
//draw the clock hour line
Line2D clockHour = new Line2D.Double();
double hourTime = (double) calendar.get(Calendar.HOUR);
lenX = HOUR_LEN*Math.sin(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));
lenY = HOUR_LEN*Math.cos(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockHour.setLine(CENTER_X, CENTER_Y, posX, posY);
g2.setColor(Color.BLUE);
g2.draw(clockHour);
int delay = 1000;
// actionListener
ActionListener drawClock;
drawClock=new ActionListener(){
public void actionPerformed(ActionEvent evt){
repaint();
}
};
//create timer
new Timer(delay, drawClock).start();
}
//variables of the panel
private int HOUR_LEN = 50;
private int MINUTE_LEN = 70;
private int SECOND_LEN = 90;
private int RADIUS = 100;
private int INNER_RADIUS = 2;
private int CENTER_X = 150;
private int CENTER_Y = 150;
}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
女生多大后可以不在长身高? 如何不用软件把手机投屏到电脑上手机屏幕怎样投放到电脑上 战时拒绝、故意延误军事订货罪既遂的处罚? 战时故意延误军事订货罪处罚标准 名师1+1导读方案:汤姆·索亚历险记目录 三星sm-g7200打开微信慢,无法正常收看,网速不慢。 笔记本电脑如何调亮屏幕亮度 大伙说说洗衣机要不要带烘干好 热烘干洗衣机怎么样 ef英语哪个好 java 简易时钟 太阳能热水器一直恒温有什么影响? 寿司醋怎么做好吃,寿司醋的家常做法 用java 编写一个时钟,要求能够走动,输入时间后显示输入的状态,按一个按钮可以恢复走动。 太阳能热水器温度显示屏上的恒温是什么意思 java程序设计 定义一个时钟类,要求如下1,创建新对象时默认0时0分0秒。 2,设置时钟为指定的时间。3,编写 Java编写世界时钟 用JAVA写数字时钟 如何用Java中做一个电子时钟 用java编写一个时钟的程序 用java编写时钟 求高人解释下每一行的意思。谢谢!!! 写一个时钟的java类 用Java编写一个时钟都需要用到什么? 预科班都学什么呢 预科生和本科生的学生差距体现在哪些方面 什么是预科班?上预科班有什么好处 为何会有大学预科生的存在?是怎么回事? 预科生的坏处 大学预科生 预科生与直接升入本科生的差距? 四季沐歌太能热水器恒温有什么作用 我家太阳能热水器这样恒温有个火字标志 这是烧电的还是用太阳的 怎么自己调配寿司醋 太阳能热水器的控制器的保温按钮是什么,用什么保温 怎么做寿司醋? 太阳能热水器如何让一池水恒温 海尔太阳能热水器恒温怎么取消 寿司醋是如何做的呢? 太阳能热水器里的恒温需要用电么 自己做寿司醋的方法和用料的比例是怎样的? 太阳能热水器不能手动加热,可以恒温加热 Win10系统磁贴无法移动怎么办 急啊..正规文档都是几倍的行间距啊?还有是先用一,再用(一),再用1吗? 谢谢 正式的公文档案的标题的字体格式,行间距,正文字体是什么? cf登录不进去跳转直播怎么办 静态ip怎么设置无线 cf怎么进游戏,进去就是直播? 如何设置静态无线局域网的IP地址? 穿越火线能登陆就是进不了游戏界面出现小块黑屏,会听见两秒的cf声音,之后就退回直播页面_百度问一问 为什么玩CF进不去游戏?