Java實(shí)現(xiàn)動態(tài)數(shù)字時鐘
本文實(shí)例為大家分享了Java實(shí)現(xiàn)動態(tài)數(shù)字時鐘的具體代碼,供大家參考,具體內(nèi)容如下
構(gòu)建:
Clock繼承 JFrame 為運(yùn)行頁面
ClockText 測試類 創(chuàng)建 Clock 對象
運(yùn)行效果:
具體實(shí)現(xiàn):
一、Clock類
- 四個JPnal 三個放時間 最后一個放日期
- 放時間的三個JPnal 分別加入 地點(diǎn) 時間 按鈕
- 最后一個按鈕添加日期
具體實(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("北京時間");
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();
// 轉(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類用于測試
public class ClockText{
public static void main(String[] args) {
new Clock();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:基于Java驗(yàn)證jwt token代碼實(shí)例
欄 目:Java
下一篇:java基于poi導(dǎo)出excel透視表代碼實(shí)例
本文標(biāo)題:Java實(shí)現(xiàn)動態(tài)數(shù)字時鐘
本文地址:http://www.jygsgssxh.com/a1/Java/8936.html
您可能感興趣的文章
- 01-10Java實(shí)現(xiàn)動態(tài)模擬時鐘
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時間
- 01-10淺談Java中真的只有值傳遞么
- 01-10Java動態(tài)顯示當(dāng)前日期和時間
- 01-10如何解決線程太多導(dǎo)致java socket連接池出現(xiàn)的問題
- 01-10使用Swing繪制動態(tài)時鐘


閱讀排行
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動態(tài)模擬時鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動態(tài)數(shù)字時鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動態(tài)顯示當(dāng)前日期和時間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法


