1

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

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(),每10s。

单击Jbutton2(暂停按钮)后需要暂停此调用..

你能帮助我吗?我在java netbeans中构建。

4

4 回答 4

3

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

于 2013-04-27T08:18:01.690 回答
2

如果您计划在重复调用的函数中更改 GUI 的元素(Hi在您的情况下),您应该使用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();
}
于 2013-04-27T08:43:22.433 回答
1

只是有在定时器时间表

语法应该或多或少

 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);

}

对不起,如果代码将包含一些错误,我是从内存中写的,我仍然半睡半醒:)

于 2013-04-27T08:24:16.770 回答
1

Java线程是并发处理和多任务处理的强大工具。你应该使用你应该需要的线程你必须谷歌搜索java线程如何创建,中断,睡眠,运行更多......在获得线程知识后,你将轻松完成java

于 2013-04-27T08:31:39.437 回答