java显示多个地区时钟_Java实现的动态数字时钟功能示例【显示世界时间】
本文實(shí)例講述了java實(shí)現(xiàn)的動(dòng)態(tài)數(shù)字時(shí)鐘功能。分享給大家供大家參考,具體如下:
構(gòu)建:
1. clock繼承 jframe 為運(yùn)行頁面
2. clocktext 測(cè)試類 創(chuàng)建 clock 對(duì)象
運(yùn)行效果:
具體實(shí)現(xiàn):
一、clock類
四個(gè)jpnal 三個(gè)放時(shí)間 最后一個(gè)放日期
放時(shí)間的三個(gè)jpnal 分別加入 地點(diǎn) 時(shí)間 按鈕
最后一個(gè)按鈕添加日期
具體實(shí)現(xiàn)如下:
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));
}
// 關(guān)系
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("北京時(shí)間");
jlabelbeijing.setfont(placefont);
jpanelbeijing.add(jlabelbeijing);
setwestpanel();
jlabel jlabelnewyork = new jlabel("紐約時(shí)間");
jlabelnewyork.setfont(placefont);
jpanelnewyork.add(jlabelnewyork);
setcenterpanel();
jlabel jlabellondon = new jlabel("倫敦時(shí)間");
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();
// 轉(zhuǎn)換日期顯示格式
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("北京繼續(xù)");
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();
// 轉(zhuǎn)換日期顯示格式
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("紐約繼續(xù)");
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(" 當(dāng)前日期: " + 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("倫敦繼續(xù)");
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(" 當(dāng)前日期: " + date.format(new date(timemillis)));
}
});
timeaction.start();
jpaneldate.add(jlabeldate);
}
private void jframeclick(){
setdefaultcloseoperation(jframe.do_nothing_on_close);//設(shè)置不默認(rèn)關(guān)閉
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, "確認(rèn)退出么?", "友情提示", 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
}
});
}
}
二、創(chuàng)建clocktext類用于測(cè)試
public class clocktext{
public static void main(string[] args) {
new clock();
}
}
ps:這里再為大家推薦幾款時(shí)間及日期相關(guān)工具供大家參考使用:
在線世界各地時(shí)間查詢:
unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:
在線日期/天數(shù)計(jì)算器:
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
希望與廣大網(wǎng)友互動(dòng)??
點(diǎn)此進(jìn)行留言吧!
總結(jié)
以上是生活随笔為你收集整理的java显示多个地区时钟_Java实现的动态数字时钟功能示例【显示世界时间】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vpi_register_cb 注册自定
- 下一篇: 怎么在CAD表格中画斜线?CAD表格斜线