JFrame 定时器(ikun专用)

package demo_2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

public class 鸡你太美定时器 extends JFrame implements ActionListener {
    private JTextField text, text1;
    private JButton startBtn, stopBtn, continueBtn;
    private Timer timer;
    private SimpleDateFormat format;//日期输出格式
    private long t;
    private long l = Long.MAX_VALUE;
    JMenu jMenu;

    JMenuItem jMenuItem;

    public 鸡你太美定时器() {
        //GUI部分
        format = new SimpleDateFormat("hh:mm:ss");
        text = new JTextField(20);//用来显示时间
        text1 = new JTextField(20);

        final long timeTnterval = 1000;

        Runnable runnable = new Runnable() {

            public void run() {
                while (true){
                    System.out.println("鸡你太美");
                    Date date = new Date(); text1.setText(format.format(date));
                    try {
                        Thread.sleep(timeTnterval);
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();

        JMenuBar menuBar = new JMenuBar();
        jMenu = new JMenu("设置");
        menuBar.add(jMenu);
        jMenuItem = new JMenuItem("定时");
        jMenu.add(jMenuItem);
        startBtn = new JButton("开始记时");
        stopBtn = new JButton("停止");
        continueBtn = new JButton("退出");
        setLayout(new FlowLayout());//设置布局
        setJMenuBar(menuBar);
        add(startBtn);
        add(stopBtn);
        add(continueBtn);
        add(text);
        add(text1);
        setSize(300, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置timer计时器,和日期格式化

        timer = new Timer(1000, this);//1s振铃一次
        //设置监听器,该类本身就是监听器,不用设置,引用可以直接拿到
        //添加监听器,3个按钮和一个timer共用一个Listener
        startBtn.addActionListener(this);
        stopBtn.addActionListener(this);
        continueBtn.addActionListener(this);
        timer.addActionListener(this);
        jMenuItem.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == timer) {
            //触发源是计时器,那么需要设置时间
            Date currentDate = new Date();
            //SimpleDateFormat s = new SimpleDateFormat();
            //String format=s.format(currentDate);
            long a = System.currentTimeMillis();
            System.out.println(a);
            if ((a - t) >= l) {
                if (!timer.isRunning()) JOptionPane.showMessageDialog(this, "已到时");
                timer.stop();
            }
            text.setText(format.format(currentDate));
        } else if (e.getSource() == startBtn) {
            //触发源是开始按钮
            timer.start();
            t = System.currentTimeMillis();
            System.out.println(t);
        } else if (e.getSource() == stopBtn) {
            //触发源是结束按钮
            timer.stop();
        } else if (e.getSource() == continueBtn) {
            //触发源是继续按钮
            System.exit(0);
        } else if (e.getSource() == jMenuItem) {

            String s = JOptionPane.showInputDialog(this, "输入秒");
            l = ((Long.valueOf(s)) * 1000);
            JOptionPane.showMessageDialog(this, "已定时" + (l / 1000) + "秒");
        }
    }


}

用户只需在test类中new一个对象即可运行!

你可能感兴趣的:(java,jvm)