如何添加毫秒到这种类型的秒表的简单代码.Java NETBEANS Jframe



程序完成了,但我想添加毫秒到它是真正的秒表的一部分。我不拥有这个代码,但我刚刚修改了它。我使用了Jframe(拖放)。如果在if/else中有毫秒的时间,它就会完成。但如何?

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class Stopwatch extends javax.swing.JFrame {
    private static int h = 0, m = 0, s = 0;
    private Timer t;
    public Stopwatch() {
         initComponents();
        this.setTitle ("Timer");
        this.setLocationRelativeTo (null);
        setIcon ();
    }

    int i = 0;
        @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
        JframeP = new javax.swing.JPanel();
        timerP = new javax.swing.JPanel();
        hours = new javax.swing.JLabel();
        colon1 = new javax.swing.JLabel();
        minutes = new javax.swing.JLabel();
        colon2 = new javax.swing.JLabel();
        seconds = new javax.swing.JLabel();
        start = new javax.swing.JButton();
        pause = new javax.swing.JButton();
        reset = new javax.swing.JButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        JframeP.setBackground(new java.awt.Color(0, 0, 0));
        JframeP.setPreferredSize(new java.awt.Dimension(600, 300));
        timerP.setBackground(new java.awt.Color(255, 255, 255));
        timerP.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        timerP.setPreferredSize(new java.awt.Dimension(552, 160));
        hours.setFont(new java.awt.Font("DS-Digital", 0, 130)); // NOI18N
        hours.setForeground(new java.awt.Color(255, 0, 102));
        hours.setText("00");
        colon1.setFont(new java.awt.Font("DS-Digital", 0, 130)); // NOI18N
        colon1.setForeground(new java.awt.Color(255, 0, 102));
        colon1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        colon1.setText(":");
        minutes.setFont(new java.awt.Font("DS-Digital", 0, 130)); // NOI18N
        minutes.setForeground(new java.awt.Color(255, 0, 102));
        minutes.setText("00");
        colon2.setFont(new java.awt.Font("DS-Digital", 0, 130)); // NOI18N
        colon2.setForeground(new java.awt.Color(255, 0, 102));
        colon2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        colon2.setText(":");
        seconds.setFont(new java.awt.Font("DS-Digital", 0, 130)); // NOI18N
        seconds.setForeground(new java.awt.Color(255, 0, 102));
        seconds.setText("00");
        javax.swing.GroupLayout timerPLayout = new javax.swing.GroupLayout(timerP);
        timerP.setLayout(timerPLayout);
        timerPLayout.setHorizontalGroup(
            timerPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, timerPLayout.createSequentialGroup()
                .addContainerGap(37, Short.MAX_VALUE)
                .addComponent(hours)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(colon1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(minutes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(colon2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(seconds)
                .addGap(33, 33, 33))
        );
        timerPLayout.setVerticalGroup(
            timerPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(hours, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
            .addComponent(colon1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(minutes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(colon2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(seconds, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        start.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        start.setText("START");
        start.setPreferredSize(new java.awt.Dimension(115, 50));
        start.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                startMouseClicked(evt);
            }
        });
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });
        pause.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        pause.setText("PAUSE");
        pause.setPreferredSize(new java.awt.Dimension(115, 50));
        pause.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pauseMouseClicked(evt);
            }
        });
        reset.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        reset.setText("RESET");
        reset.setPreferredSize(new java.awt.Dimension(115, 50));
        reset.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                resetMouseClicked(evt);
            }
        });
        javax.swing.GroupLayout JframePLayout = new javax.swing.GroupLayout(JframeP);
        JframeP.setLayout(JframePLayout);
        JframePLayout.setHorizontalGroup(
            JframePLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JframePLayout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(start, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(82, 82, 82)
                .addComponent(pause, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(reset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46))
            .addGroup(JframePLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(timerP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );
        JframePLayout.setVerticalGroup(
            JframePLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JframePLayout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(timerP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(JframePLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(start, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pause, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(reset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(JframeP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(JframeP, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>                        

    private void startMouseClicked(java.awt.event.MouseEvent evt) {                                   
      t=new Timer(1000,new ActionListener(){
         public void actionPerformed(ActionEvent ae){
            if (s < 59) {
                s++;
                String sec = (s < 10 ? "0" : "") + s;
                seconds.setText("" + sec);
            } else {
                s = 0;
                seconds.setText("00");
                if (m < 59) {
                    m++;
                    String min = (m < 10 ? "0" : "") + m;
                    minutes.setText("" + min);
                } else {
                    h = 0;
                    minutes.setText("00");
                    h++;
                    String hour = (h < 10 ? "0" : "") + h;
                    hours.setText("" + hour);
                }
            }
         } 
      });
      t.start();
        start.setEnabled (false);
    }                                  

    private void pauseMouseClicked(java.awt.event.MouseEvent evt) {                                   
     if (i == 0) {
            t.stop();
            i = 1;
            pause.setText("CONTINUE");
        } else {
            t.start();
            i = 0;
            pause.setText("PAUSE");}
    }                                  
    private void resetMouseClicked(java.awt.event.MouseEvent evt) {                                   
        t.stop();
        hours.setText("00"); h = 0;
        minutes.setText("00"); m = 0;
        seconds.setText("00"); s = 0;
        i = 0;
        pause.setText("PAUSE");
        start.setEnabled (true);
    }                                  
    private void startActionPerformed(java.awt.event.ActionEvent evt) {                                      
    }                                     
        public void actionPerformed(ActionEvent ae) {
        }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Stopwatch().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JPanel JframeP;
    private javax.swing.JLabel colon1;
    private javax.swing.JLabel colon2;
    private javax.swing.JLabel hours;
    private javax.swing.JLabel minutes;
    private javax.swing.JButton pause;
    private javax.swing.JButton reset;
    private javax.swing.JLabel seconds;
    private javax.swing.JButton start;
    private javax.swing.JPanel timerP;
    // End of variables declaration                   
    private void setIcon() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Stopwatch.png")));
    }
}

当你初始化计时器对象

 t=new Timer(1000,new ActionListener(){
 public void actionPerformed(ActionEvent ae){
    // stuff
 }

你告诉计时器,等待1000毫秒,然后执行actionPerformed。为了让它每毫秒执行一次动作你可以把它改成

t=new Timer(1,new ActionListener() 
public void actionPerformed(ActionEvent ae){
        // stuff
}

之后,您可以引入类似于已经存在的用于处理毫秒的代码,

提示一秒钟有1000毫秒,所以代码将类似于秒处理,只是不是每60秒增加分钟,而是每1000毫秒增加秒。

编辑:

因为你没有写代码,如果你想知道定时器是在哪里启动的,它的方法:

private void startMouseClicked(java.awt.event.MouseEvent evt) {...}

相关内容

  • 没有找到相关文章