Java实现动态数字时钟
作者:_yuanhao 时间:2022-05-07 08:56:56
本文实例为大家分享了Java实现动态数字时钟的具体代码,供大家参考,具体内容如下
构建:
Clock继承 JFrame 为运行页面
ClockText 测试类 创建 Clock 对象
运行效果:
具体实现:
一、Clock类
四个JPnal 三个放时间 最后一个放日期
放时间的三个JPnal 分别加入 地点 时间 按钮
最后一个按钮添加日期
具体实现如下:
public class Clock extends JFrame {
private JPanel jPanelBeijing;
private JPanel jPanelNewYork;
private JPanel jPanelLondom;
private JPanel jPanelDate;
private boolean BeijingThreadFlag_IsStart = true;
private boolean NewYorkThreadFlag_IsStart = true;
private boolean LondonThreadFlag_IsStart = true;
public Clock() {
// TODO Auto-generated constructor stub
jPanelBeijing = new JPanel();
jPanelNewYork = new JPanel();
jPanelLondom = new JPanel();
jPanelDate = new JPanel();
iniRelations();
iniLayout();
jFrameClick();
setVisible(true);
setSize(480, 225);
setLocationRelativeTo(null);
}
private void iniLayout() {
jPanelBeijing.setLayout(new GridLayout(3, 1));
jPanelNewYork.setLayout(new GridLayout(3, 1));
jPanelLondom.setLayout(new GridLayout(3, 1));
}
// 关系
private void iniRelations() {
this.add(BorderLayout.WEST, jPanelBeijing);
this.add(BorderLayout.CENTER, jPanelNewYork);
this.add(BorderLayout.EAST, jPanelLondom);
this.add(BorderLayout.SOUTH, jPanelDate);
Font placeFont = new Font("楷体", Font.BOLD, 36);
JLabel jLabelBeijing = new JLabel("北京时间");
jLabelBeijing.setFont(placeFont);
jPanelBeijing.add(jLabelBeijing);
setWestPanel();
JLabel jLabelNewYork = new JLabel("纽约时间");
jLabelNewYork.setFont(placeFont);
jPanelNewYork.add(jLabelNewYork);
setCenterPanel();
JLabel jLabelLondon = new JLabel("伦敦时间");
jLabelLondon.setFont(placeFont);
jPanelLondom.add(jLabelLondon);
setEastPanel();
setDatePanel();
}
private void setWestPanel() {
// add time for SouthPanel
JLabel jLabelTime = new JLabel("加载中.");
jLabelTime.setFont(new Font("宋体", Font.BOLD, 30));
Timer timeAction = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
long timemillis = System.currentTimeMillis();
// 转换日期显示格式
SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss ");
jLabelTime.setText(time.format(new Date(timemillis)));
}
});
timeAction.start();
jPanelBeijing.add(jLabelTime);
Button button = new Button("北京暂停");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (BeijingThreadFlag_IsStart) {
timeAction.stop();
button.setLabel("北京继续");
BeijingThreadFlag_IsStart = false;
} else {
timeAction.start();
button.setLabel("北京暂停");
BeijingThreadFlag_IsStart = true ;
}
}
});
jPanelBeijing.add(button);
}
private void setCenterPanel() {
// add time for SouthPanel
JLabel jLabelTime = new JLabel("加载中.");
jLabelTime.setFont(new Font("宋体", Font.BOLD, 30));
Timer timeAction = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
long timemillis = System.currentTimeMillis();
// 转换日期显示格式
SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss ");
jLabelTime.setText(time.format(new Date(timemillis - 13 * 60 * 60 * 1000)));
}
});
timeAction.start();
jPanelNewYork.add(jLabelTime);
Button button = new Button("纽约暂停");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (NewYorkThreadFlag_IsStart) {
timeAction.stop();
button.setLabel("纽约继续");
NewYorkThreadFlag_IsStart = false;
} else {
timeAction.start();
button.setLabel("纽约暂停");
NewYorkThreadFlag_IsStart = true ;
}
}
});
jPanelNewYork.add(button);
}
private void setEastPanel() {
// add time for SouthPanel
// JLabel jLabelDate = new JLabel("Date");
JLabel jLabelTime = new JLabel("加载中.");
jLabelTime.setFont(new Font("宋体", Font.BOLD, 30));
Timer timeAction = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
long timemillis = System.currentTimeMillis();
// SimpleDateFormat date = new SimpleDateFormat("yyyy 年 MM 月 dd
// 日 ");
// jLabelDate.setText(" 当前日期: " + date.format(new
// Date(timemillis)));
SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss ");
jLabelTime.setText(time.format(new Time(timemillis - 8 * 60 * 60 * 1000)));
}
});
timeAction.start();
jPanelLondom.add(jLabelTime);
Button button = new Button("伦敦暂停");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (LondonThreadFlag_IsStart) {
timeAction.stop();
button.setLabel("伦敦继续");
LondonThreadFlag_IsStart = false;
} else {
timeAction.start();
button.setLabel("伦敦暂停");
LondonThreadFlag_IsStart = true ;
}
}
});
jPanelLondom.add(button);
// jPanelLondom.add(jLabelDate);
}
private void setDatePanel() {
// add time for SouthPanel
JLabel jLabelDate = new JLabel("加载中.");
Timer timeAction = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
long timemillis = System.currentTimeMillis();
SimpleDateFormat date = new SimpleDateFormat("yyyy 年 MM 月 dd 日 ");
jLabelDate.setText(" 当前日期: " + date.format(new Date(timemillis)));
}
});
timeAction.start();
jPanelDate.add(jLabelDate);
}
private void jFrameClick(){
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//设置不默认关闭
addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
int x = JOptionPane.showConfirmDialog(null, "确认退出么?", "友情提示", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (x == 0) {
System.exit(0);
}
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
});
}
}
二、创建ClockText类用于测试
public class ClockText{
public static void main(String[] args) {
new Clock();
}
}
来源:https://blog.csdn.net/qq_43377749/article/details/85096893
标签:java,时钟
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java使用@Scheduled注解执行定时任务
2021-09-16 08:37:16
![](https://img.aspxhome.com/file/2023/6/65786_0s.jpg)
java实现十六进制字符unicode与中英文转换示例
2021-05-29 18:19:29
MongoDB中ObjectId的误区及引起的一系列问题
2023-06-16 09:46:00
![](https://img.aspxhome.com/file/2023/1/110621_0s.png)
基于@RequestBody和@ResponseBody及Stringify()的作用说明
2021-12-14 01:47:38
![](https://img.aspxhome.com/file/2023/6/69106_0s.png)
java调用ffmpeg实现转换视频
2022-08-25 15:21:04
spring @schedule注解如何动态配置时间间隔
2023-07-29 03:51:14
Android虚拟机与类加载机制详情
2022-12-16 20:09:03
![](https://img.aspxhome.com/file/2023/4/106024_0s.webp)
Spring Boot缓存实战 EhCache示例
2023-08-30 12:23:35
![](https://img.aspxhome.com/file/2023/3/58213_0s.png)
SpringMVC使用RESTful接口案例
2021-10-10 20:03:45
![](https://img.aspxhome.com/file/2023/0/80920_0s.jpg)
java利用mybatis拦截器统计sql执行时间示例
2021-08-22 11:35:41
Android自定义可控制速度的跑马灯
2023-08-11 14:05:00
详解JAVA中获取文件MD5值的四种方法
2021-07-20 22:56:15
![](https://img.aspxhome.com/file/2023/1/78221_0s.png)
jar包运行时提示jar中没有主清单属性的解决
2023-11-23 19:04:10
![](https://img.aspxhome.com/file/2023/2/59532_0s.png)
Android中获取资源 id 及资源 id 的动态获取
2023-06-30 04:38:06
C#完成word文档打印的方法
2023-05-29 23:55:36
![](https://img.aspxhome.com/file/2023/0/77290_0s.png)
C#异步调用实例小结
2023-07-16 10:31:03
论C++的lambda是函数还是对象
2022-09-24 05:16:06
浅谈JSON的数据交换、缓存问题和同步问题
2021-08-08 09:31:25
Android开发中amera2 Preview使用详解
2023-11-09 20:17:56
C#实现二叉排序树代码实例
2021-10-10 06:26:12
![](https://img.aspxhome.com/file/2023/3/102223_0s.png)