用java编写一个计数器或计时器

用java编写一个计数器或计时器,第1张

import java.awt.BorderLayout

import java.awt.Container

import java.awt.Font

import java.awt.GridLayout

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JOptionPane

import javax.swing.JPanel

import javax.swing.JTextField

public class TimerDemo extends JFrame implements ActionListener {

    private static final long serialVersionUID = 201306211111L

    private JTextField screen = new JTextField("0")

    private JButton start = new JButton("开始")

    private JButton reset = new JButton("重置")

    private JPanel panel = new JPanel()

    private boolean isRunning

    private int time

    private int timeBetween

    public TimerDemo(int timeBetween) {

        super("计时器")

        this.timeBetween = timeBetween

        try {

            init()

        } catch (Exception e) {

            e.printStackTrace()

        }

    }

    public TimerDemo() {

        super("计时器")

        this.timeBetween = 100

        try {

            init()

        } catch (Exception e) {

            e.printStackTrace()

        }

    }

    private void init() {

        panel.setLayout(new GridLayout())

        panel.add(start)

        panel.add(reset)

        start.addActionListener(this)

        reset.addActionListener(this)

        screen.setFont(new Font("幼圆", Font.BOLD, 60))

        screen.setHorizontalAlignment(JTextField.CENTER)

        screen.setEditable(false)

        Container c = getContentPane()

        c.setLayout(new BorderLayout())

        c.add(panel, BorderLayout.SOUTH)

        c.add(screen, BorderLayout.CENTER)

        this.setSize(200, 150)

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

        this.setResizable(false)

        this.setLocationRelativeTo(null)

        this.setVisible(true)

    }

    public static void main(String[] args) {

        new TimerDemo(1)// 设定 1ms/次

        // new TimerDemo()

    }

    @Override

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == start) {

            if (start.getText().equals("开始")) {

                start.setText("暂停")

                isRunning = true

            } else if (start.getText().equals("暂停")) {

                start.setText("开始")

                isRunning = false

            }

        }

        if (e.getSource() == reset) {

            start.setText("开始")

            screen.setText("0")

            isRunning = false

            time = 0

        }

        new Thread(new TimeZone()).start()

    }

    class TimeZone implements Runnable {

        @Override

        public void run() {

            while (isRunning) {

                time++

                if (time >= Integer.MAX_VALUE) {

                    screen.setText("ERROR")

                    JOptionPane.showMessageDialog(null, "ERROR")

                    isRunning = false

                }

                screen.setText(String.valueOf(time))

                try {

                    Thread.sleep(timeBetween)

                } catch (Exception e) {

                    e.printStackTrace()

                }

            }

        }

    }

}

class Computer{

    int value

    Computer(int value){

        this.value=value

    }

    public void add(){

        System.out.println("Value:"+value+"->"+(value+1))

        value++

    }

    public void sub(){

        System.out.println("Value:"+value+"->"+(value-2))

        value-=2

    }

    public void clear(){

        System.out.println("Value:"+value+"->"+0)

        value=0

    }

}

public class Demo{

    public static void main(String[] args){

        Computer computer=new Computer(10)

        computer.add()

        computer.sub()

        computer.clear()

    }

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11823794.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存