简单的计时器程序

时间:2013-04-27 08:12:54

标签: java timer

我有简单的Jframe和2个按钮。

public class Animace extends javax.swing.JFrame {


    public Animace() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Play");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Pause");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addContainerGap(237, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.LINE_END);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 319, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
     // PLAY BUTTON


    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        //PAUSE BUTTON
    }


    public static void Hi(){
       System.out.println("Hi guys");
    }

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Animace().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration
}

点击Jbutton1(播放按钮)后需要调用方法Hi(),每隔10秒。

点击Jbutton2(暂停按钮)后需要暂停此次通话..

你能帮帮我吗?我建立了java netbeans。

4 个答案:

答案 0 :(得分:3)

有java教程Swing Timer,它解释了如何使用Swing Timer,这就是你需要的。

答案 1 :(得分:2)

如果您计划在重复调用的函数(在您的情况下为Hi)中更改GUI的元素,则应使用javax.swing.Timer

private javax.swing.Timer timer;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    timer = new Timer(10*1000, new java.awt.event.ActionListener() {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            Hi();
        }
    });
    timer.start();        
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    timer.stop();
}

答案 2 :(得分:1)

只需拥有计时器schedule

语法应该或多或少

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
     // PLAY BUTTON
//make it field so you can stop it
Timer t = new Timer();
TimerTask task = new TimerTask(){
   public void run()
    {
      Hi();
    }
  }
t.scheduler(task,0,10*1000);

}

抱歉,如果代码中包含一些错误,即可从内存中写入内容,而且我只能半睡一觉。)

答案 3 :(得分:1)

Java线程是并发处理和多任务处理的强大工具。你应该使用线程,你应该需要你必须google java线程如何创建,中断,睡眠,运行更多...你将很容易用java完成线程知识后