中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java 定時器類 Timer 使用方法

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
 
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Timer;
import javax.swing.*;
 
public class SimpleTimer extends JWindow {

 
    public static void main(String[] args) {

        SimpleTimer simpleTimer = new SimpleTimer();
        simpleTimer.pack();
        simpleTimer.setAlwaysOnTop(true);
        simpleTimer.setVisible(true);
    }

    private Long startTimeMillis;

    private JLabel label;

    private SimpleDateFormat dateFormat;

    private Timer timer;

 
    public SimpleTimer() {

        initDateFormat();
        initConponents();
        resetTime();
        updateLabelText();
        startTimer();
    }
 
    private void initDateFormat() {

        dateFormat = new SimpleDateFormat("HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    }

 
    private void initConponents() {

        label = new JLabel();
        label.setFont(new Font(label.getFont().getName(), Font.BOLD, 14));
        label.setOpaque(true);
        label.setBackground(Color.black);
        label.setForeground(Color.white);
        label.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
        getContentPane().add(label);

        final JPopupMenu popup = new JPopupMenu();
        JMenuItem menuItem1 = new JMenuItem("Reset");
        menuItem1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                resetTime();
            }
        });
        popup.add(menuItem1);
        JMenuItem menuItem2 = new JMenuItem("Exit");
        menuItem2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                System.exit(0);
            }
        });
        popup.add(menuItem2);
        label.setComponentPopupMenu(popup);

        label.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {

                if (e.getButton() == MouseEvent.BUTTON2
                        && e.getClickCount() == 1) {
                    popup.setVisible(true);
                }
            }
        });
    }

 
    private void resetTime() {

        startTimeMillis = Calendar.getInstance().getTimeInMillis();
    }

    /**
     * ラベル更新
     */
    private void updateLabelText() {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                long elapsedTime = Calendar.getInstance().getTimeInMillis()
                        - startTimeMillis;

                Date date = new Date(elapsedTime);

                label.setText(dateFormat.format(date));
            }
        });
    }

    /**
     * タイマー開始
     */
    private void startTimer() {

        timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {

                updateLabelText();

            }
        }, 0, 1000);
    }
}
 

標簽:

版權申明:本站文章部分自網(wǎng)絡,如有侵權,請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:JavaScript實現(xiàn)圖片預加載

下一篇:js高級截取字符串功能