NetBeans的计数降低了毫秒(Timer / Stopwatch项目)

时间:2019-05-27 10:11:54

标签: java netbeans-8

NetBeans中的Stopwatch / Timer项目计数的milisecods(秒)比应该慢的

我做过和这个家伙一样的项目。没有错误...它的速度大约是速度的两倍... https://www.youtube.com/watch?v=LoKQvAQpL3w 代码:

package stoperica;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Stoperica.java
 *
 * Created on May 15, 2019, 6:51:35 PM
 */
/**
 *
 * @author 1199
 */
public class Stoperica extends javax.swing.JFrame {

    /** Creates new form Stoperica */

    static int sat = 0;
    static int minut = 0;
    static int sekunda = 0;
    static int milisek = 0;

    static boolean state = true;

    public Stoperica() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        sat_label = new javax.swing.JLabel();
        milisek_label = new javax.swing.JLabel();
        minut_label = new javax.swing.JLabel();
        sekunda_label = new javax.swing.JLabel();
        bstart = new javax.swing.JButton();
        bpauza = new javax.swing.JButton();
        breset = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        sat_label.setFont(new java.awt.Font("SansSerif", 1, 36));
        sat_label.setText("00 :");

        milisek_label.setFont(new java.awt.Font("SansSerif", 1, 18));
        milisek_label.setText("00");

        minut_label.setFont(new java.awt.Font("SansSerif", 1, 36));
        minut_label.setText("00 :");
        minut_label.setToolTipText("");

        sekunda_label.setFont(new java.awt.Font("SansSerif", 1, 36));
        sekunda_label.setText("00.");

        bstart.setBackground(new java.awt.Color(-15740401,true));
        bstart.setFont(new java.awt.Font("SansSerif", 1, 18));
        bstart.setText("Start");
        bstart.setToolTipText("");
        bstart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bstartActionPerformed(evt);
            }
        });

        bpauza.setBackground(new java.awt.Color(-1048576,true));
        bpauza.setFont(new java.awt.Font("SansSerif", 1, 18));
        bpauza.setText("Pauza");
        bpauza.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bpauzaActionPerformed(evt);
            }
        });

        breset.setBackground(new java.awt.Color(-987136,true));
        breset.setFont(new java.awt.Font("SansSerif", 1, 18));
        breset.setText("Reset");
        breset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bresetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(63, 63, 63)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(sat_label, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bstart, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(minut_label, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sekunda_label)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(milisek_label, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bpauza)
                        .addGap(18, 18, 18)
                        .addComponent(breset))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(80, 80, 80)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sekunda_label, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(minut_label, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sat_label, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(milisek_label, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bstart, javax.swing.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(bpauza, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                        .addComponent(breset, javax.swing.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)))
                .addGap(89, 89, 89))
        );

        pack();
    }// </editor-fold>                        

    private void bstartActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:

    state = true;

    Thread t = new Thread()
    {
        public void run()
            {
                for(;;)
                    {
                        if(state == true)
                            {
                                try
                                {
                                    sleep(1);

                                    if (milisek>600)
                                    {
                                        milisek = 0;
                                        sekunda++;
                                    }

                                    if (sekunda>60)
                                    {
                                        milisek = 0;
                                        sekunda = 0;
                                        minut++;
                                    }

                                    if (minut>60)
                                    {
                                        milisek = 0;
                                        sekunda = 0;
                                        minut = 0;
                                        sat++;
                                    }


                                    milisek_label.setText(""+milisek);
                                    milisek++;

                                    if(sekunda < 10) {
                                        sekunda_label.setText("0"+sekunda + "."); 
                                    } else {
                                        sekunda_label.setText(sekunda + ".");
                                    }
                                    if(minut < 10) {
                                        minut_label.setText("0"+minut + " :"); 
                                    } else {
                                        minut_label.setText(minut + " :");
                                    }
                                    if(sat < 10) {
                                        sat_label.setText("0"+sat+" :"); 
                                    } else {
                                        sat_label.setText(""+sat+" :");
                                    }


                                }
                                catch(InterruptedException e)
                                {

                                }
                        } else {
                            break;
                        }
                    }     
             }
    };
    t.start();  
    }                                      

private void bpauzaActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
    state = false;    
}                                      

private void bresetActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
    state = false;

    sat = 0;
    minut = 0;
    sekunda = 0;
    milisek = 0;

    sat_label.setText("00 :");
    minut_label.setText("00 :");
    sekunda_label.setText("00.");
    milisek_label.setText("00");
}                                      

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Stoperica().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton bpauza;
    private javax.swing.JButton breset;
    private javax.swing.JButton bstart;
    private javax.swing.JLabel milisek_label;
    private javax.swing.JLabel minut_label;
    private javax.swing.JLabel sat_label;
    private javax.swing.JLabel sekunda_label;
    // End of variables declaration                   
}

0 个答案:

没有答案