开发者

Button whith show counter and switch anoter frame after 60 second

hello friends how to create counter button in swing i am using code to switch the frame after 30 second but it will not display 30 second

so how to do that ?

thanks

package javaapplication1;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TimerTask;
import java.util.Timer;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TimerButton extends JFrame {

    Timer timernew;

    javax.swing.Timer timer;

    public TimerButton() {
        initComponents();
    }

    private TimerButton(int seconds){
        initComponents();
        timernew = new Timer();
        timer = new javax.swing.Timer(1000, new MyActionListener());
        timer.setInitialDelay(0);
        timer.start();
        timernew.schedule(new RemindTask(), seconds*1000);
        System.out.println(timernew.toString());

    }

 class MyAct开发者_如何学编程ionListener implements ActionListener {
        private int counter = 10;

        public void actionPerformed(ActionEvent e) {
            counter = counter - 1;
            String text = "<html><font size=\"14\">" + String.valueOf(counter) + "</font></head>";
            counterLabel.setText(text);

            if (counter == 0) {
                timer.stop();
            }
        }
    }

    class RemindTask extends TimerTask {
        public void run() {
            System.out.format("Time's up!%n");

            jButton1ActionPerformed(null);
            timernew.cancel(); //Terminate the timer thread
     }

    }

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

        jButton1 = new javax.swing.JButton();
        counterLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

        counterLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(90, 90, 90)
                        .addComponent(counterLabel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56)
                .addComponent(counterLabel)
                .addContainerGap(78, Short.MAX_VALUE))
        );

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

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

      test234 te = new test234();
      te.setVisible(true);

    }                                        

public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TimerButton(10).setVisible(true);
            }
        });
}



    // Variables declaration - do not modify
    private javax.swing.JLabel counterLabel;
    private javax.swing.JButton jButton1;
    // End of variables declaration

}


i provide code that is example of counter button which will call some frame after 30 second..

import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

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

/* * NewJFrame.java * * Created on Feb 10, 2011, 10:57:13 AM */

/** * * @author win xp */ public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */
public NewJFrame() {
    initComponents();
     initUI();
}

private javax.swing.Timer timer = null; private void initUI() {

    timer = new javax.swing.Timer(1000, (ActionListener) new MyActionListener());


    timer.setInitialDelay(0);
    timer.start();
}
   class MyActionListener implements ActionListener {
    private int counter = 10;

    public void actionPerformed(ActionEvent e) {
        counter = counter - 1;
        String text = "<html><font size=\"14\">" + String.valueOf(counter) + "</font></head>";
        counterLabel.setText(text);

        if (counter == 0) {
            timer.stop();
            jButton1ActionPerformed(null);


        }
    }
}
/** 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() {

    jButton1 = new javax.swing.JButton();
    counterLabel = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

    counterLabel.setText("jLabel1");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(149, 149, 149)
                    .addComponent(jButton1))
                .addGroup(layout.createSequentialGroup()
                    .addGap(163, 163, 163)
                    .addComponent(counterLabel)))
            .addContainerGap(178, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(77, 77, 77)
            .addComponent(counterLabel)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
            .addComponent(jButton1)
            .addGap(105, 105, 105))
    );

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

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

    Show show = new Show();
    show.setVisible(true);
    this.setVisible(false);
    timer.stop();

}                                        

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel counterLabel;
private javax.swing.JButton jButton1;
// End of variables declaration                   

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜