【Java】Java计时器(秒表),java基础面试笔试题


我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家。
扫描二维码或搜索下图红色VX号,加VX好友,拉你进【程序员面试学习交流群】免费领取。也欢迎各位一起在群里探讨技术。
推荐文章:Java 面试知识点解析;Mysql优化技巧(数据库设计、命名规范、索引优化

 

https://blog.csdn.net/c_jian/article/details/50506759

 

应用名称:Java计时器

用到的知识:Java GUI编程

开发环境:win8+eclipse+jdk1.8

功能说明:计时功能,精确到1毫秒,可暂停。

效果图:

源代码:

 

import javax.swing.*;  

import java.awt.HeadlessException;  

import java.awt.BorderLayout;  

import java.awt.FlowLayout;  

import java.awt.Font;  

import java.awt.event.ActionListener;  

import java.awt.event.ActionEvent;  

   

/** 

 * 计时器 

 */  

public class Timer extends JFrame {  

   

    /**

     * 

     */

    private static final long serialVersionUID = 1L;

 

    private static final String INITIAL_LABEL_TEXT = "00:00:00 000";  

   

    // 计数线程  

    private CountingThread thread = new CountingThread();  

   

    // 记录程序开始时间  

    private long programStart = System.currentTimeMillis();  

   

    // 程序一开始就是暂停的  

    private long pauseStart = programStart;  

   

    // 程序暂停的总时间  

    private long pauseCount = 0;  

   

    private JLabel label = new JLabel(INITIAL_LABEL_TEXT);  

   

    private JButton startPauseButton = new JButton("开始");  

   

    private JButton resetButton = new JButton("清零");  

   

    private ActionListener startPauseButtonListener = new ActionListener() {  

        public void actionPerformed(ActionEvent e) {  

            if (thread.stopped) {  

                pauseCount += (System.currentTimeMillis() - pauseStart);  

                thread.stopped = false;  

                startPauseButton.setText("暂停");  

            } else {  

                pauseStart = System.currentTimeMillis();  

                thread.stopped = true;  

                startPauseButton.setText("继续");  

            }  

        }  

    };  

   

    private ActionListener resetButtonListener = new ActionListener() {  

        public void actionPerformed(ActionEvent e) {  

            pauseStart = programStart;  

            pauseCount = 0;  

            thread.stopped = true;  

            label.setText(INITIAL_LABEL_TEXT);  

            startPauseButton.setText("开始");  

        }  

    };  

   

    public Timer(String title) throws HeadlessException {  

        super(title);  

        setDefaultCloseOperation(EXIT_ON_CLOSE);  

        setLocation(300, 300);  

        setResizable(false);  

   

        setupBorder();  

        setupLabel();  

        setupButtonsPanel();  

   

        startPauseButton.addActionListener(startPauseButtonListener);  

        resetButton.addActionListener(resetButtonListener);  

   

        thread.start(); // 计数线程一直就运行着  

    }  

   

    // 为窗体面板添加边框  

    private void setupBorder() {  

        JPanel contentPane = new JPanel(new BorderLayout());  

        contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));  

        this.setContentPane(contentPane);  

    }  

   

    // 配置按钮  

    private void setupButtonsPanel() {  

        JPanel panel = new JPanel(new FlowLayout());  

        panel.add(startPauseButton);  

        panel.add(resetButton);  

        add(panel, BorderLayout.SOUTH);  

    }  

   

    // 配置标签  

    private void setupLabel() {  

        label.setHorizontalAlignment(SwingConstants.CENTER);  

        label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(), 40));  

        this.add(label, BorderLayout.CENTER);  

    }  

   

    // 程序入口  

    public static void main(String[] args) {  

        try {  

            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  

        } catch (Exception e) {  

            e.printStackTrace();  

        }  

   

        Timer frame = new Timer("计时器");  

        frame.pack();  

        frame.setVisible(true);  

    }  

   

    private class CountingThread extends Thread {  

   

        public boolean stopped = true;  

   

        private CountingThread() {  

            setDaemon(true);  

        }  

   

        @Override  

        public void run() {  

            while (true) {  

                if (!stopped) {  

                    long elapsed = System.currentTimeMillis() - programStart - pauseCount;  

                    label.setText(format(elapsed));  

                }  

   

                try {  

                    sleep(1);  // 1毫秒更新一次显示

                } catch (InterruptedException e) {  

                    e.printStackTrace();  

                    System.exit(1);  

                }  

            }  

        }  

   

        // 将毫秒数格式化  

        private String format(long elapsed) {  

            int hour, minute, second, milli;  

   

            milli = (int) (elapsed % 1000);  

            elapsed = elapsed / 1000;  

   

            second = (int) (elapsed % 60);  

            elapsed = elapsed / 60;  

   

            minute = (int) (elapsed % 60);  

            elapsed = elapsed / 60;  

   

            hour = (int) (elapsed % 60);  

   

            return String.format("%02d:%02d:%02d %03d", hour, minute, second, milli);  

        }  

    }  

}  

 

 


转载:https://www.cnblogs.com/yxdmoodoo/p/9244635.html

推荐内容:
java 三种工厂模式
Java修行之路
Java面试题(二)
BATJ面试必会之Java IO 篇
Java面试集合(三)
[JAVA实现]微信公众号网页授权登录
Java入门篇(四)——数组
Java多线程同步问题:一个小Demo完全搞懂
java后端实习生面试题目
java面试题总结

 

你可能感兴趣的:(java)