swing - Button whith show counter and switch anoter frame after 60 second -
hello friends how create counter button in swing using code switch frame after 30 second not display 30 second
so how ?
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 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(); } } } class remindtask extends timertask { public void run() { system.out.format("time's up!%n"); jbutton1actionperformed(null); timernew.cancel(); //terminate 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 - not modify private javax.swing.jlabel counterlabel; private javax.swing.jbutton jbutton1; // end of variables declaration }
i provide code example of counter button call frame after 30 second..
import java.awt.event.actionevent; import java.awt.event.actionlistener;
/* * change template, choose tools | templates * , open template in editor. */
/* * newjframe.java * * created on feb 10, 2011, 10:57:13 */
/** * * @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); } } } /** method called within constructor * initialize form. * warning: not modify code. content of method * regenerated 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 command line arguments */ public static void main(string args[]) { java.awt.eventqueue.invokelater(new runnable() { public void run() { new newjframe().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jlabel counterlabel; private javax.swing.jbutton jbutton1; // end of variables declaration
}
Comments
Post a Comment