• 多线程简单运用---时钟的实现


    效果图:

    代码:

    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("时钟");
    
    	}
    
    }
    


     

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

  • 相关阅读:
    Spring使用Jackson处理json数据
    手工搭建web项目
    购物车模块
    C# ——利用反射动态加载dll
    C# —— 利用Marshal.GetDelegateForFunctionPointer 来转换一个函数指针为一个委托
    C# —— GetProcAddress函数检索指定的动态链接库(DLL)中的输出库函数地址。
    c#——IntPtr
    C#-StructLayoutAttribute(结构体布局)
    C#报错——传递数组对象报错“未将对象引用设置到对象的实例”
    C#——保留小数点,强转
  • 原文地址:https://www.cnblogs.com/dingxiaoyue/p/4931861.html
Copyright © 2020-2023  润新知