• <i id='p2lb4tf7'><tr id='ieg3kkwd'><dt id='7isio5s7'><q id='0e54kboe'><span id='c3o7p4u0'><b id='n5t8noor'><form id='ce7nk6uk'><ins id='7advlitp'></ins><ul id='7cwjn03j'></ul><sub id='q9qcbiyc'></sub></form><legend id='dgcqa7xu'></legend><bdo id='yam2b3sm'><pre id='2a8ajjqk'><center id='xzbh5p9t'></center></pre></bdo></b><th id='72bn06m1'></th></span></q></dt></tr></i><div id='o42d1efj'><tfoot id='z6sugwq4'></tfoot><dl id='vjoqaa5o'><fieldset id='sw1f17v7'></fieldset></dl></div>

      <bdo id='qk3779g4'></bdo><ul id='6a7xe7oz'></ul>

      <small id='idi4kqrn'></small><noframes id='a2vwc16q'>

      <tfoot id='9qn5s3y3'></tfoot>
    1. <legend id='0xzn7tje'><style id='mqyy212m'><dir id='x8353fg7'><q id='kzwe0x9n'></q></dir></style></legend>

        欢迎来到入门教程网!

        Java编程

        当前位置:主页 > 软件编程 > Java编程 >

        java计时开关代码 java计时程序

        来源:本站原创|时间:2023-04-09|栏目:Java编程|点击:

        求java的计时器代码,应该比较简单的,来看看吧。

        package test;

        import java.util.*;

        import java.awt.*;

        import java.awt.event.*;

        import java.applet.*;

        public class Test5 extends Applet {

            private final Panel pan = new Panel();

            private final Label time = new Label();

            private final Button btnGo = new Button("开始");

            private final Button btnPouse = new Button("暂停");

            private final Button btnReset = new Button("复位");

            private final StopwatchThread swThread = new StopwatchThread();

           

            private class btnGoListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

           

        swThread.go();

        btnGo.setEnabled(false);

        }

            }

            private class btnPouseListener implements ActionListener {

             public void actionPerformed(ActionEvent e) {

                if(btnGo.isEnabled()){

                return ;

                }

              if (btnPouse.getLabel().equals("继续")) {

             swThread.go();

             btnPouse.setLabel("暂停");

                 } else if (btnPouse.getLabel().equals("暂停")) {

             swThread.noGo();

             btnPouse.setLabel("继续");

                 }

             }

                }

            private class btnResetListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            swThread.reset();

            btnGo.setEnabled(true);

            btnGo.setLabel("开始");

            btnPouse.setLabel("暂停");

        }

            }

            private class StopwatchThread extends Thread {

        private boolean going = false;

        private long prevElapsed = 0;

        private Date startDate = new Date();

        private long elapsedTime() {

            return prevElapsed +

        (going ? new Date().getTime() - startDate.getTime() : 0);

        }

        private String msToString(long time) {

           System.out.println(time+"  "+((0*60+2)*1000+999));

        if(((99*60+59)*1000+983)=time((99*60+59)*1000+999)=time){//((0*60+2)*1000+983)=time((0*60+2)*1000+999)=time

        if (time % 1000  990)

        time += 2;

             swThread.noGo();

            }

        String ms, sec, min;

            if (time % 10 = 5)

        time += 5;

            ms = Long.toString(time % 1000);

            while (ms.length()  3)

        ms = "0" + ms;

            ms = ms.substring(0, ms.length() - 1);

            time /= 1000;

            sec = Long.toString(time % 60);

            if (sec.length() == 1) sec = "0" + sec;

            time /= 60;

            min = Long.toString(time);

            

            return min + ":" + sec + "." + ms;

        }

        public void go() {

            startDate = new Date();

            going = true;

        }

        public void noGo() {

            prevElapsed = elapsedTime();

            going = false;

        }

        public void reset() {

            going = false;

            prevElapsed = 0;

        }

        public void run() {

            while (true) {

        time.setText(msToString(elapsedTime()));

        yield();

            }

        }

            }

            public void init() {

        setLayout(new GridLayout(2,2));

        setBackground(Color.lightGray);

        setForeground(Color.black);

        pan.setLayout(new GridLayout(3,2));

        pan.add(new Label("计时:"));

        time.setForeground(Color.blue);

        pan.add(time);

        pan.add(btnGo);

        pan.add(btnPouse);

        pan.add(btnReset);

        pan.add(new Label());

        add(pan);

        btnGo.addActionListener(new btnGoListener());

        btnReset.addActionListener(new btnResetListener());

        btnPouse.addActionListener(new btnPouseListener());

        swThread.setDaemon(true);

        swThread.start();

            }

            public static void main(String[] args) {

            Test5 applet = new Test5();

        Frame aFrame = new Frame("计时器");

        aFrame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {

        System.exit(0);

            }

        });

        aFrame.add(applet, BorderLayout.CENTER);

        aFrame.setSize(400, 200);

        applet.init();

        applet.start();

        aFrame.setVisible(true);

            }

        }

        可以改变有注释的那个if语句里面的值来判断什么时候停止

        求人用java编写一条计时器代码。

        import java.awt.BorderLayout;

        import java.awt.Container;

        import java.awt.GridLayout;

        import java.awt.event.ActionEvent;

        import java.awt.event.ActionListener;

        import java.awt.event.WindowEvent;

        import java.awt.event.WindowListener;

        import java.io.*;

        import java.util.*;

        import javax.swing.JButton;

        import javax.swing.JFrame;

        import javax.swing.JOptionPane;

        import javax.swing.JPanel;

        import javax.swing.JTextField;

        import javax.swing.plaf.OptionPaneUI;

        public class Demo {

        static boolean isRuning=false;

        static boolean isFirst=true;

        @SuppressWarnings("unchecked")

        public static void main(String[] args) throws Exception {

        JFrame form1 = new JFrame("Form1");

        JTextField jTextField = new JTextField(10);

        jTextField.setSize(10, 10);

        jTextField.setText("0");

        jTextField.setEditable(false);

        JButton jButton = new JButton("开始");

        jButton.setSize(10, 10);

        Thread thread = new Thread(new Runnable() {

        @Override

        public void run() {

        while (true) {

        while(isRuning){

        Integer counter = Integer.parseInt(jTextField.getText().trim());

        counter++;

        jTextField.setText(counter.toString());

        try {

        Thread.sleep(1000);

        } catch (Exception e2) {

        }

        }

        }

        }

        });

        jButton.addActionListener(new ActionListener() {

        @Override

        public void actionPerformed(ActionEvent e) {

           String text=jButton.getText().equals("开始")?"暂停":"开始";

           jButton.setText(text);

           isRuning=!isRuning;

           if(isFirst){

           thread.start();

           isFirst=false;

           }

        }

        });

        JPanel panel = new JPanel();

        panel.setSize(200, 200);

        panel.add(jTextField, BorderLayout.NORTH);

        panel.add(jButton, BorderLayout.CENTER);

        form1.add(panel);

        form1.setBounds(200, 100, 250, 150);

        form1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        form1.addWindowListener(new WindowListener() {

        @Override

        public void windowOpened(WindowEvent e) {

        // TODO Auto-generated method stub

        }

        @Override

        public void windowIconified(WindowEvent e) {

        // TODO Auto-generated method stub

        }

        @Override

        public void windowDeiconified(WindowEvent e) {

        // TODO Auto-generated method stub

        }

        @Override

        public void windowDeactivated(WindowEvent e) {

        // TODO Auto-generated method stub

        }

        @Override

        public void windowClosing(WindowEvent e) {

                       // 窗口关闭前取出文本框的数字保存到外部文件,代码在此处写

        JOptionPane.showMessageDialog(null, "Are you sure closing?");

        }

        @Override

        public void windowClosed(WindowEvent e) {

        // TODO Auto-generated method stub

        }

        @Override

        public void windowActivated(WindowEvent e) {

        // TODO Auto-generated method stub

        }

        });

        form1.setVisible(true);

        }

        }

        写一个计时器 JAVA代码是什么?

        应该用线程里面的Timer来控制package com.sy.game.test;

        import java.util.Timer;

        import java.util.TimerTask;

        public class TimeTask {

        public static void main(String[] args) {

        TimeTask tTask=new TimeTask();

        tTask.timeVoid();

        }

        public void timeVoid(){

        final Timer timer = new Timer();

        TimerTask tt=new TimerTask() {

        @Override

        public void run() {

        System.out.println("到点啦!");

        timer.cancel();

        }

        };

        timer.schedule(tt, 3000);

        }

        }

        整合的:

        /*

        * java倒计时器

        * shiyang

        * */

        package com.sy.game.test;

        import java.awt.Container;

        import java.awt.FlowLayout;

        import java.awt.Toolkit;

        import java.awt.event.ActionEvent;

        import java.awt.event.ActionListener;

        import javax.swing.JButton;

        import javax.swing.JFrame;

        import javax.swing.JPanel;

        import javax.swing.JTextField;

        import javax.swing.Timer;

        @SuppressWarnings("unused")

        public class TimeController extends JFrame implements ActionListener {

        private static final long serialVersionUID = 4603262282860990473L;

        private static final int DEFAULT_WIDTH = 200;

        private static final int DEFAULT_HEIGHT = 100;

        private static final int width = Toolkit.getDefaultToolkit()

        .getScreenSize().width;

        private static final int height = Toolkit.getDefaultToolkit()

        .getScreenSize().height;

        private Container container;

        private JButton btn;

        private JTextField jtfTime;

        private Timer tmr;

        public TimeController() {

        initComponents();

        Timer tmr = new Timer(1000, this);

        this.tmr = tmr;

        setVisible(true);

        }

        private void initComponents() {

        this.setTitle("SY秒表");

        this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        this.setResizable(false);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setLocation((width - DEFAULT_WIDTH) / 2,

        (height - DEFAULT_HEIGHT) / 2);

        jtfTime = new JTextField("10");

        btn = new JButton("开始倒计时");

        container = getContentPane();

        JPanel panel = new JPanel();

        panel.add(btn);

        panel.add(jtfTime);

        this.add(panel);

        btn.addActionListener(this);

        }

        public void actionPerformed(ActionEvent ae) {

        if (ae.getSource() == btn) {

        jtfTime.setText("10");

        tmr.start();

        } else {

        int t;

        t = Integer.parseInt(jtfTime.getText());

        t--;

        jtfTime.setText("" + t);

        if (t = 0) {

        tmr.stop();

        }

        }

        }

        public static void main(String[] args) {

        TimeController timeController = new TimeController();

        }

        }

          <legend id='cbpe9nhy'><style id='lgp3fwxv'><dir id='ctaswr4a'><q id='s5ge3nxx'></q></dir></style></legend>

          <tfoot id='0tuovza3'></tfoot>
        • <i id='chsb6sqz'><tr id='ld0j5fti'><dt id='gpmdsya6'><q id='0bfv5fol'><span id='nf2w4nzj'><b id='336k31f1'><form id='95aunsp8'><ins id='fzg4jk93'></ins><ul id='qxiiy6aw'></ul><sub id='1z9vpz90'></sub></form><legend id='uamdfc0v'></legend><bdo id='ujyvxogt'><pre id='nkh2thab'><center id='ag1ua4vd'></center></pre></bdo></b><th id='3vm63a5k'></th></span></q></dt></tr></i><div id='dhkw031m'><tfoot id='8s2clz7v'></tfoot><dl id='2jwby27a'><fieldset id='3a2je06o'></fieldset></dl></div>

            <tbody id='cgcf2379'></tbody>
            1. <small id='7mjrepcj'></small><noframes id='u73mwzn9'>

                • <bdo id='lagapeu0'></bdo><ul id='sjtaixyv'></ul>

                  上一篇:java代码重构优化经验 java代码重构的思路

                  栏    目:Java编程

                  下一篇:没有了

                  本文标题:java计时开关代码 java计时程序

                  本文地址:https://www.xiuzhanwang.com/a1/Javabiancheng/17333.html

                  网页制作CMS教程网络编程软件编程脚本语言数据库服务器

                  如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

                  联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

                  Copyright © 2002-2020 脚本教程网 版权所有

                  <i id='q8ur1eu4'><tr id='3etd6cox'><dt id='nvrt7tam'><q id='2m5cfcqz'><span id='y2fcwote'><b id='nat9oqdk'><form id='qzgk0vdm'><ins id='obcq8zs1'></ins><ul id='yy8lg6rn'></ul><sub id='yls4ruoy'></sub></form><legend id='a1o1y7sf'></legend><bdo id='oqxhfh7i'><pre id='ro8zum4a'><center id='18kaawvk'></center></pre></bdo></b><th id='xb14ksr8'></th></span></q></dt></tr></i><div id='1j92wdf4'><tfoot id='x4xzypi8'></tfoot><dl id='t4tnuyov'><fieldset id='zivmz2ja'></fieldset></dl></div>

                  <legend id='26437w4l'><style id='vca7qajf'><dir id='1ysne0kn'><q id='03j2h520'></q></dir></style></legend>
                    <bdo id='vsd8fg0j'></bdo><ul id='0t6ykxo0'></ul>

                  <small id='huo0u7ea'></small><noframes id='wezv4xnv'>

                • <tfoot id='sf0dx99v'></tfoot>