java多线程世界时钟_Java实现的动态数字时钟功能示例【显示世界时间】
本文实例讲述了Java实现的动态数字时钟功能。分享给大家供大家参考,具体如下:
构建:
1. Clock继承 JFrame 为运行页面
2. 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();
}
}
PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:
希望本文所述对大家java程序设计有所帮助。
java多线程世界时钟_Java实现的动态数字时钟功能示例【显示世界时间】相关推荐
- java显示多个地区时钟_Java实现的动态数字时钟功能示例【显示世界时间】
本文实例讲述了java实现的动态数字时钟功能.分享给大家供大家参考,具体如下: 构建: 1. clock继承 jframe 为运行页面 2. clocktext 测试类 创建 clock 对象 运行效 ...
- java动态时钟_Java实现的动态数字时钟功能示例【显示世界时间】|chu
本文实例讲述了Java实现的动态数字时钟功能.分享给大家供大家参考,具体如下: 构建: 1. Clock继承 JFrame 为运行页面 2. ClockText 测试类 创建 Clock 对象 运行效 ...
- java的简单数字时钟_Java实现的简单数字时钟功能示例
本文实例讲述了java实现的简单数字时钟功能.分享给大家供大家参考,具体如下: 应用名称:java数字时钟 用到的知识:java gui编程,线程 开发环境:win8+eclipse+jdk1.8 功 ...
- java监控任务进度_Java Swing组件实现进度监视功能示例
本文实例讲述了java swing组件实现进度监视功能.分享给大家供大家参考,具体如下: 实例一: import java.awt.event.actionevent; import java.awt ...
- java如何实现动态时钟_Java实现动态数字时钟
本文实例为大家分享了Java实现动态数字时钟的具体代码,供大家参考,具体内容如下 构建: Clock继承 JFrame 为运行页面 ClockText 测试类 创建 Clock 对象 运行效果: 具体 ...
- java数字时钟_Java实现动态数字时钟
本文实例为大家分享了Java实现动态数字时钟的具体代码,供大家参考,具体内容如下 构建: Clock继承 JFrame 为运行页面 ClockText 测试类 创建 Clock 对象 运行效果: 具体 ...
- android 时钟动态图标,Android 8.1 Launcher3实现动态指针时钟功能
本文主要实现功能,可能有不合理的地方 首先创建一个实现功能的工具里,直接上代码: import android.content.Context; import android.graphics.Bit ...
- Android动态秒针插件app,Android 8.1 Launcher3实现动态指针时钟功能
本文主要实现功能,可能有不合理的地方 首先创建一个实现功能的工具里,直接上代码: import android.content.Context; import android.graphics.Bit ...
- 基于51单片机的自动浇花系统设计/基于51单片机的智能抽奖系统控制设计/基于51单片机的数字时钟与日历显示控制设计 毕业设计
1147基于51单片机的自动浇花系统设计 设计思路:通湿度传感器实时监测湿度,通过LCD显示出实时的湿度采集值,可以通过按键设定目标界限,当达到这个界限值时,浇花系统的电机装置运行. 电路包含:LCD ...
最新文章
- Hinton一作新论文:如何在神经网络中表示“部分-整体层次结构”?
- CentOS 6.5系统下安装和配置NFS服务
- SSRS - 请求因 HTTP 状态 401 失败: Unauthorized。
- python利器怎么编程-bluepy 一款python封装的BLE利器简单介绍
- 有了 Docker,用 JavaScript 框架开发的 Web 站点也能很好地支持网络爬虫的内容抓取...
- Oracle数据库----函数
- 好玩的Scratch
- docker java镜像_Springboot整合MongoDB的Docker开发,其它应用也类似
- Magento调用静态块 static block
- Linux命令解释之grep
- 六自由度机械臂项目学习笔记
- 奇迹s12源码_muserver 奇迹MU服务端VC++源码
适合研究 的朋友 Game Simulator 模拟 器 269万源代码下载- www.pudn.com...
- 计算机用户密码查看器,电脑开机密码查看工具
- 怎么把m4a文件变成mp3格式
- 【电子书推荐】这几年看过的不错的电子书单
- 7-1 复数类的操作
- 服务器装win10 稳定吗,其实不必太过纠结 谈谈Mac装不装Win10
- 联咏NT98321 NT98323 _SDK编译
- git clone 克隆成功,但签出失败
- 阿里云视频点播PHP sdk上传文件