zoukankan      html  css  js  c++  java
  • 多线程简单运用---时钟的实现

    效果图:

    代码:

    package game;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Calendar;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    public class ShowClock extends JFrame {
    
    	Container c;
    	JMenuBar mb;
    	JMenu JFile,JHelp;
    	JMenuItem mExit,mCopyright;
    	JLabel clock;
    	JPanel pl;
    	public void init(){
    	   c=this.getContentPane();
    	   c.setLayout(new BorderLayout());
    	
    	   mb=new JMenuBar();
    	   c.add(mb,BorderLayout.NORTH);
    	   
    	   JFile=new JMenu("文件(F)");
    	   JHelp=new JMenu("帮助(H)");
    	   mb.add(JFile);mb.add(JHelp);
    	   mExit=new JMenuItem("退出");
    	   mCopyright=new JMenuItem("版权信息");
    	   JFile.add(mExit);JHelp.add(mCopyright);
    	   
    	   mCopyright.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    			   JOptionPane.showMessageDialog(null, "本游戏由暗伤无痕出品", "版权声明", JOptionPane.INFORMATION_MESSAGE);   
    		   }
    	   });
    	   mExit.addActionListener(new ActionListener(){
    		   public void actionPerformed(ActionEvent e){
    			   ShowClock.this.dispose();
    		   }
    	   });
    	   pl=new JPanel();pl.setLayout(null);
    	   c.add(pl);
    	   clock=new JLabel();
    	   clock.setFont(new Font("黑体",Font.BOLD,15));
           clock.setForeground(Color.blue);
    	   pl.add(clock);
    	   clock.setBounds(40,20,400,30);
    	   ShowTime st=new ShowTime(clock);
            st.start();
    	   this.setSize(500,400);
    	   this.setVisible(true);
    	}
    	
    	public ShowClock(String title){
    		super(title);
    		init();
    	}
    	class ShowTime extends Thread{
    		JLabel clock;
    		public ShowTime(JLabel clock){
    			this.clock=clock;
    		}
    		public void run(){
    			while(true){
    		  Calendar c=Calendar.getInstance();
    		  int year=c.get(Calendar.YEAR);
    		  int month=c.get(Calendar.MONTH);
    		  int day=c.get(Calendar.DATE);
    		  int hour=c.get(Calendar.HOUR_OF_DAY);
    		  int minute=c.get(Calendar.MINUTE);
    		  int second=c.get(Calendar.SECOND);
    		  String s="北京时间   "+year+"年"+month+"月"+day+"日"+hour+"时"+minute+"分"+second+"秒";
    		  clock.setText(s);
    			}
    			}
    	}
    	public static void main(String[] args) {
    	    ShowClock sc=new ShowClock("时钟");
    
    	}
    
    }
    


     

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    【转载】时域信号的频谱、功率谱和功率谱密度计算
    【转载】Chaotic Time-Series Prediction
    【转载】Restarting an analysis in ANSYS
    Interpolation in MATLAB
    【转载】ANSYS 动力分析 (9)
    在oracle中 将一个以逗号隔开的String字符串转换成以单引号逗号隔开的集合
    在java中获取时间,单位毫秒。主要用于查看执行时间效率
    比较两个数组元素是否相等
    oracle 从一张表的数据复制到另一张表中 mapper.xml
    打印错误栈信息:可以捕获具体错误信息
  • 原文地址:https://www.cnblogs.com/dingxiaoyue/p/4931861.html
Copyright © 2011-2022 走看看