Swing-定时器

Swing-定时器_第1张图片

Swing-定时器_第2张图片 

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

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

 

你可能感兴趣的:(Java【渡劫】,Swing高级,Java,Swing(GUI)图形界面)