Swing-定时器

 

 点击开始按钮,实现显示当前时间

package my2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;

public class MyFrame extends JFrame
{
	JLabel disPlay=new JLabel("--:--:--");
	
	//注:import javax.swing.Timer;
	Timer timer;
	
	public MyFrame(String title)
	{
		super(title);
		
		JPanel root=new JPanel();
		this.setContentPane(root);
		root.setLayout(new BorderLayout());
		
		disPlay.setFont(new Font("宋体",Font.PLAIN,60));
		disPlay.setHorizontalAlignment(SwingConstants.CENTER);
		disPlay.setOpaque(true);
		disPlay.setBackground(Color.white);
		disPlay.setForeground(Color.BLUE);
		root.add(disPlay, BorderLayout.CENTER);
		
		Box toolBar=Box.createHorizontalBox();
		JButton startButton=new JButton("开始");
		JButton endButton=new JButton("停止");
		
		//工具栏
		toolBar.add(startButton);
		toolBar.add(Box.createHorizontalStrut(30));
		toolBar.add(endButton);
		root.add(toolBar,BorderLayout.PAGE_START);
		
		startButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				onStart();	
			}
		});
		
		endButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				onEnd();	
			}
		});
		
	
	}
	
	//开始按钮
	private void onStart()
	{
		if(timer!=null)return;
		
		//创建定时器,每隔1000毫秒执行一次(更新一次)
		
		ActionListener task=new UpdateTask();
		timer=new Timer(1000,task);
		timer.start();
		
	}
	
	//停止按钮
	private void onEnd()
	{
		if(timer!=null)
		{
			timer.stop();
			timer=null;	
		}
		disPlay.setText("--:--:--");
	}
	
	private class UpdateTask implements ActionListener
	{

		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			//1、actionPerformed()在界面工作线程里运行,所以必须迅速返回
			//2、在这里可以直接更新UI
			SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");
			String text=sdf.format(System.currentTimeMillis());
			disPlay.setText(text);
		}
		
	}
}
package my2;

import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Demo
{
	private static void createGUI()
	{
		// JFrame指一个窗口,构造方法的参数为窗口标题
		// 语法:因为MyFrame是JFrame的子类,所以可以这么写
		JFrame frame = new MyFrame("Swing Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		// 设置窗口的其他参数,如窗口大小
		frame.setSize(700, 450);
		
		// 显示窗口
		frame.setVisible(true);
		
		
	}
	
	public static void main(String[] args)
	{
		//设置界面样式 Look And Feel
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
				| UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run()
			{
				createGUI();
			}
		});
 
	}
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值