swing - java help, Total and Enter buttons in the class are not working, I tried to review the action listener for both buttons -
/** java class works sales simulator, have difficulty working total , enter buttons, problem total , enter buttons, cannot figure why these buttons not working!! */
// import applications java library
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; // starting cashregister class public class cashregister extends jframe { //initiate value double subtotal = 0.0; // stores subtotal of items purchased // jlabel , jtextfield enter amount private jlabel amountjlabel; private jtextfield amountjtextfield; // jbuttons enter numbers in amountjtextfield private jbutton onejbutton; private jbutton twojbutton; private jbutton threejbutton; private jbutton fourjbutton; private jbutton fivejbutton; private jbutton sixjbutton; private jbutton sevenjbutton; private jbutton eightjbutton; private jbutton ninejbutton; private jbutton zerojbutton; private jbutton pointjbutton; // jbutton add value in amountjtextfield subtotal private jbutton enterjbutton; // jbutton determine tax , calculate final total private jbutton totaljbutton; // jbutton delete value displayed in amountjtextfield private jbutton deletejbutton; // jbutton clear results private jbutton clearjbutton; // jlabel , jtextfield display subtotal private jlabel subtotaljlabel; private jtextfield subtotaljtextfield; // jlabel , jtextfield display tax private jlabel taxjlabel; private jtextfield taxjtextfield; // jlabel , jtextfield display final total private jlabel totaljlabel; private jtextfield totaljtextfield; // decimalformat format dollar amounts private decimalformat dollars = new decimalformat( "$0.00" ); // no-argument constructor public cashregister() { createuserinterface(); } // create , position gui components; register event handlers private void createuserinterface() { // content pane attaching gui components container contentpane = getcontentpane(); // enable explicit positioning of gui components contentpane.setlayout( null ); // set amountjlabel amountjlabel = new jlabel(); amountjlabel.setbounds( 20, 20, 15, 20 ); amountjlabel.settext( "$" ); contentpane.add( amountjlabel ); // set amountjtextfield amountjtextfield = new jtextfield(); amountjtextfield.setbounds( 40, 20, 260, 20 ); contentpane.add( amountjtextfield ); // set onejbutton onejbutton = new jbutton(); onejbutton.setbounds( 55, 70, 45, 20 ); onejbutton.settext( "1" ); contentpane.add( onejbutton ); onejbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when onejbutton pressed public void actionperformed( actionevent event ) { onejbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set twojbutton twojbutton = new jbutton(); twojbutton.setbounds( 100, 70, 45, 20 ); twojbutton.settext( "2" ); contentpane.add( twojbutton ); twojbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when twojbutton pressed public void actionperformed( actionevent event ) { twojbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set threejbutton threejbutton = new jbutton(); threejbutton.setbounds( 145, 70, 45, 20 ); threejbutton.settext( "3" ); contentpane.add( threejbutton ); threejbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when threejbutton pressed public void actionperformed( actionevent event ) { threejbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set fourjbutton fourjbutton = new jbutton(); fourjbutton.setbounds( 55, 90, 45, 20 ); fourjbutton.settext( "4" ); contentpane.add( fourjbutton ); fourjbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when fourjbutton pressed public void actionperformed( actionevent event ) { fourjbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set fivejbutton fivejbutton = new jbutton(); fivejbutton.setbounds( 100, 90, 45, 20 ); fivejbutton.settext( "5" ); contentpane.add( fivejbutton ); fivejbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when fivejbutton pressed public void actionperformed( actionevent event ) { fivejbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set sixjbutton sixjbutton = new jbutton(); sixjbutton.setbounds( 145, 90, 45, 20 ); sixjbutton.settext( "6" ); contentpane.add( sixjbutton ); sixjbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when sixjbutton pressed public void actionperformed( actionevent event ) { sixjbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set sevenjbutton sevenjbutton = new jbutton(); sevenjbutton.setbounds( 55, 110, 45, 20 ); sevenjbutton.settext( "7" ); contentpane.add( sevenjbutton ); sevenjbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when sevenjbutton pressed public void actionperformed( actionevent event ) { sevenjbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set eightjbutton eightjbutton = new jbutton(); eightjbutton.setbounds( 100, 110, 45, 20 ); eightjbutton.settext( "8" ); contentpane.add( eightjbutton ); eightjbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when eightjbutton pressed public void actionperformed( actionevent event ) { eightjbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set ninejbutton ninejbutton = new jbutton(); ninejbutton.setbounds( 145, 110, 45, 20 ); ninejbutton.settext( "9" ); contentpane.add( ninejbutton ); ninejbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when ninejbutton pressed public void actionperformed( actionevent event ) { ninejbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set zerojbutton zerojbutton = new jbutton(); zerojbutton.setbounds( 100, 130, 45, 20 ); zerojbutton.settext( "0" ); contentpane.add( zerojbutton ); zerojbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when zerojbutton pressed public void actionperformed( actionevent event ) { zerojbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set pointjbutton pointjbutton = new jbutton(); pointjbutton.setbounds( 145, 130, 45, 20 ); pointjbutton.settext( "." ); contentpane.add( pointjbutton ); pointjbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when pointjbutton pressed public void actionperformed( actionevent event ) { pointjbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set enterjbutton enterjbutton = new jbutton(); enterjbutton.setbounds( 205, 70, 70, 20 ); enterjbutton.settext( "enter" ); contentpane.add( enterjbutton ); enterjbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when enterjbutton pressed public void actionperformed( actionevent event ) { enterjbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set totaljbutton totaljbutton = new jbutton(); totaljbutton.setbounds( 205, 90, 70, 20 ); totaljbutton.settext( "total" ); contentpane.add( totaljbutton ); totaljbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when totaljbutton pressed public void actionperformed( actionevent event ) { totaljbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set deletejbutton deletejbutton = new jbutton(); deletejbutton.setbounds( 205, 110, 70, 20 ); deletejbutton.settext( "delete" ); contentpane.add( deletejbutton ); deletejbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when deletejbutton pressed public void actionperformed( actionevent event ) { deletejbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set clearjbutton clearjbutton = new jbutton(); clearjbutton.setbounds( 205, 130, 70, 20 ); clearjbutton.settext( "clear" ); contentpane.add( clearjbutton ); clearjbutton.addactionlistener( new actionlistener() // anonymous inner class { // event handler called when clearjbutton pressed public void actionperformed( actionevent event ) { clearjbuttonactionperformed( event ); } } // end anonymous inner class ); // end call addactionlistener // set subtotaljlabel; subtotaljlabel = new jlabel(); subtotaljlabel.setbounds( 20, 180, 90, 20 ); subtotaljlabel.settext( "subtotal:" ); contentpane.add( subtotaljlabel ); // set subtotaljtextfield subtotaljtextfield = new jtextfield(); subtotaljtextfield.setbounds( 160, 180, 140, 20 ); subtotaljtextfield.sethorizontalalignment( jtextfield.right ); subtotaljtextfield.seteditable( false ); subtotaljtextfield.settext( "$0.00" ); contentpane.add( subtotaljtextfield ); // set taxjlabel taxjlabel = new jlabel(); taxjlabel.setbounds( 20, 220, 60, 20 ); taxjlabel.settext( "tax:" ); contentpane.add( taxjlabel ); // set taxjtextfield taxjtextfield = new jtextfield(); taxjtextfield.setbounds( 160, 220, 140, 20 ); taxjtextfield.sethorizontalalignment( jtextfield.right ); taxjtextfield.seteditable( false ); taxjtextfield.settext( "$0.00" ); contentpane.add( taxjtextfield ); // set totaljlabel totaljlabel = new jlabel(); totaljlabel.setbounds( 20, 260, 70, 20 ); totaljlabel.settext( "total:" ); contentpane.add( totaljlabel ); // set totaljtextfield totaljtextfield = new jtextfield(); totaljtextfield.setbounds( 160, 260, 140, 20 ); totaljtextfield.sethorizontalalignment( jtextfield.right ); totaljtextfield.seteditable( false ); totaljtextfield.settext( "$0.00" ); contentpane.add( totaljtextfield ); // set properties of application's window settitle( "cash register" ); // set window title setsize( 345, 330 ); // set window size setvisible( true ); // display window } // end method createuserinterface // append "0" amount in amountjtextfield private void zerojbuttonactionperformed( actionevent event ) { amountjtextfield.settext( amountjtextfield.gettext() + "0" ); } // end method zerojbuttonactionperformed // append "1" amount in amountjtextfield private void onejbuttonactionperformed( actionevent event ) { amountjtextfield.settext( amountjtextfield.gettext() + "1" ); } // end method onejbuttonactionperformed // append "2" amount in amountjtextfield private void twojbuttonactionperformed( actionevent event ) { amountjtextfield.settext( amountjtextfield.gettext() + "2" ); } // end method twojbuttonactionperformed // append "3" amount in amountjtextfield private void threejbuttonactionperformed( actionevent event ) { amountjtextfield.settext( amountjtextfield.gettext() + "3" ); } // end method threejbuttonactionperformed // append "4" amount in amountjtextfield private void fourjbuttonactionperformed( actionevent event ) { amountjtextfield.settext( amountjtextfield.gettext() + "4" ); } // end method fourjbuttonactionperformed // append "5" amount in amountjtextfield private void fivejbuttonactionperformed( actionevent event ) { amountjtextfield.settext( amountjtextfield.gettext() + "5" ); } // end method fivejbuttonactionperformed // append "6" amount in amountjtextfield private void sixjbuttonactionperformed( actionevent event ) { amountjtextfield.settext( amountjtextfield.gettext() + "6" ); } // end method sixjbuttonactionperformed // append "7" amount in amountjtextfield private void sevenjbuttonactionperformed( actionevent event ) { amountjtextfield.settext( amountjtextfield.gettext() + "7" ); } // end method sevenjbuttonactionperformed // append "8" amount in amountjtextfield private void eightjbuttonactionperformed( actionevent event ) { amountjtextfield.settext( amountjtextfield.gettext() + "8" ); } // end method eightjbuttonactionperformed // append "9" amount in amountjtextfield private void ninejbuttonactionperformed( actionevent event ) { amountjtextfield.settext( amountjtextfield.gettext() + "9" ); } // end method ninejbuttonactionperformed // append "." amount in amountjtextfield private void pointjbuttonactionperformed( actionevent event ) { amountjtextfield.settext( amountjtextfield.gettext() + "." ); } // end method pointjbuttonactionperformed // calculate , display subtotal private void enterjbuttonactionperformed( actionevent event ) { } // end method enterjbuttonactionperformed // calculate , display total after taxes private void totaljbuttonactionperformed( actionevent event ) { } // end method totaljbuttonactionperformed // clear current amount displayed in amountjtextfield private void deletejbuttonactionperformed( actionevent event ) { amountjtextfield.settext( "" ); } // end method deletejbuttonactionperformed // clear results private void clearjbuttonactionperformed( actionevent event ) { amountjtextfield.settext( "" ); subtotaljtextfield.settext( "$0.00" ); taxjtextfield.settext( "$0.00" ); totaljtextfield.settext( "$0.00" ); subtotal = 0.0; // reset subtotal 0.0 } // end method clearjbuttonactionperformed // main method public static void main( string[] args ) { cashregister application = new cashregister(); application.setdefaultcloseoperation( jframe.exit_on_close ); } // end method main }/** end of program*/
i think simple way show actionlistener. if understand basics of this, you'll able fix own code :)
package actionlistenerexample; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; public class actionlistenerexample extends jframe implements actionlistener{ jpanel panel = new jpanel(); jbutton button1; jbutton button2; public actionlistenerexample(){ setvisible(true); setdefaultcloseoperation(jframe.exit_on_close); add(panel); button1 = new jbutton("button1"); button1.addactionlistener(this); button2 = new jbutton("button2"); button2.addactionlistener(this); panel.add(button1); panel.add(button2); pack(); } public static void main(string[] args) { jframe jf = new actionlistenerexample(); } @override public void actionperformed(actionevent event){ if(event.getsource() == button1){ system.out.println("button1 clicked"); } if(event.getsource() == button2){ system.out.println("button2 clicked"); } } }
Comments
Post a Comment