⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 java实现的有关定时器的源码
💻 JAVA
字号:
package clock;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.Timer; 
import java.util.TimerTask; 
import java.util.Date;
import java.text.SimpleDateFormat;

public class MainFrame extends JFrame 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -2666043665155322323L;
	private final Timer timer = new Timer(); 
	private final int seconds ; 
	private JLabel nowLabel = new JLabel("现在时间是:");
	private JLabel timeLabel = new JLabel("");
	private SimpleDateFormat sdf = null;
	MainFrame()
	{
		sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.seconds = 1;
		createGUI();
	}
	private void createGUI()
	{
		this.setLayout(null);
		this.add(nowLabel);
		nowLabel.setBounds(10, 10, 70, 20);
		this.add(timeLabel);
		timeLabel.setBounds(80, 10, 120, 20);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(210, 60);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.start();
	}
	public void start()
	{ 
		timer.schedule(new TimerTask() 
		{ 
			public void run() { 
				curTime(); 
				//timer.cancel(); 
			} 
			private void curTime() 
			{ 
				timeLabel.setText(sdf.format(new Date()));
				System.out.println(sdf.format(new Date()));
/*				try {
					Thread.sleep(1500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}*/
			} 
		}, 0, seconds * 1000); 
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -