java - GUI Reservation Equation -


im doin program reserving seat bus. school project. on part of computing fee. example: reserve 3 seats click buttons reserve. textbox shoud display total costs of 3 seats. made accumulator variable expecting hold total of buttons pressed transaction multiply price.

package busreservationproject; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import javax.swing.jframe; import javax.swing.jlabel; import java.awt.event.actionlistener; import java.awt.event.actionevent; import javax.swing.jtextfield; import javax.swing.jbutton; import javax.swing.jpanel;       public class reservationsubclass extends jframe {  private jlabel a1;  private jlabel a2; private jlabel b1;  private jlabel b2; private jlabel c1;  private jlabel c2; private jlabel d1;  private jlabel d2; private jlabel e1;  private jlabel e2; private jlabel f1;  private jlabel f2; private jlabel g1;  private jlabel g2; private jlabel h1;  private jlabel h2; private jlabel i1;  private jlabel i2; private jlabel j1;  private jlabel j2;    private jtextfield counter; private jtextfield bill; private jlabel counterlab;  private jlabel blabel; private jbutton r1button; private jbutton r11button; private jbutton r2button; private jbutton r12button; private jbutton r3button; private jbutton r13button; private jbutton r4button; private jbutton r14button; private jbutton r5button; private jbutton r15button; private jbutton r6button; private jbutton r16button; private jbutton r7button; private jbutton r17button; private jbutton r8button; private jbutton r18button; private jbutton r9button; private jbutton r19button; private jbutton r10button; private jbutton r20button;  private jpanel contentpane;  private jbutton billbutton;     public reservationsubclass() {      super("bus reservation");     string string = "";       contentpane = new jpanel();     contentpane.setlayout(new gridbaglayout());     gridbagconstraints cons = new gridbagconstraints();     add(contentpane);      cons.insets = new insets (0,0,10,10);     a1 = new jlabel("a1");     a1.settooltiptext("click button below reserve seat");     r1button = new jbutton("reserve a1");     cons.gridx = 0;     cons.gridy = 1;     contentpane.add(a1,cons);     cons.gridx = 0;     cons.gridy = 2;     contentpane.add(r1button,cons);      b1 = new jlabel("b1");     b1.settooltiptext("click button below reserve seat");     r2button = new jbutton("reserve b1");      cons.gridx = 0;     cons.gridy = 3;     contentpane.add(b1,cons);     cons.gridx = 0;     cons.gridy = 4;     contentpane.add(r2button,cons);      c1 = new jlabel("c1");     c1.settooltiptext("click button below reserve seat");     r3button = new jbutton("reserve c1");      cons.gridx = 0;     cons.gridy = 5;     contentpane.add(c1,cons);     cons.gridx = 0;     cons.gridy = 6;     contentpane.add(r3button,cons);      d1 = new jlabel("d1");     d1.settooltiptext("click button below reserve seat");     r4button = new jbutton("reserve d1");      cons.gridx = 0;     cons.gridy = 7;     contentpane.add(d1,cons);     cons.gridx = 0;     cons.gridy = 8;     contentpane.add(r4button,cons);      e1 = new jlabel("e1");     e1.settooltiptext("click button below reserve seat");     r5button = new jbutton("reserve e1");      cons.gridx = 0;     cons.gridy = 9;     contentpane.add(e1,cons);     cons.gridx = 0;     cons.gridy = 10;     contentpane.add(r5button,cons);      f1 = new jlabel("f1");     f1.settooltiptext("click button below reserve seat");     r6button = new jbutton("reserve f1");      cons.gridx = 0;     cons.gridy = 11;     contentpane.add(f1,cons);     cons.gridx = 0;     cons.gridy = 12;     contentpane.add(r6button,cons);      g1 = new jlabel("g1");     g1.settooltiptext("click button below reserve seat");     r7button = new jbutton("reserve g1");      cons.gridx = 0;     cons.gridy = 13;     contentpane.add(g1,cons);     cons.gridx = 0;     cons.gridy = 14;     contentpane.add(r7button,cons);      h1 = new jlabel("h1");     h1.settooltiptext("click button below reserve seat");     r8button = new jbutton("reserve h1");      cons.gridx = 0;     cons.gridy = 15;     contentpane.add(h1,cons);     cons.gridx = 0;     cons.gridy = 16;     contentpane.add(r8button,cons);      i1 = new jlabel("i1");     i1.settooltiptext("click button below reserve seat");     r9button = new jbutton("reserve i1");      cons.gridx = 0;     cons.gridy = 17;     contentpane.add(i1,cons);     cons.gridx = 0;     cons.gridy = 18;     contentpane.add(r9button,cons);       j1 = new jlabel("j1");     j1.settooltiptext("click button below reserve seat");     r10button = new jbutton("reserve j1");      cons.gridx = 0;     cons.gridy = 19;     contentpane.add(j1,cons);     cons.gridx = 0;     cons.gridy = 20;     contentpane.add(r10button,cons);       //col 2      a2 = new jlabel("a2");     a2.settooltiptext("click button below reserve seat");     r11button = new jbutton("reserve a2");                  cons.gridx = 100;     cons.gridy = 1;     contentpane.add(a2,cons);     cons.gridx = 100;     cons.gridy = 2;     contentpane.add(r11button,cons);      b2 = new jlabel("b2");     b2.settooltiptext("click button below reserve seat");     r12button = new jbutton("reserve b2");                  cons.gridx = 100;     cons.gridy = 3;     contentpane.add(b2,cons);     cons.gridx = 100;     cons.gridy = 4;     contentpane.add(r12button,cons);      c2 = new jlabel("c2");     c2.settooltiptext("click button below reserve seat");     r13button = new jbutton("reserve c2");                  cons.gridx = 100;     cons.gridy = 5;     contentpane.add(c2,cons);     cons.gridx = 100;     cons.gridy = 6;     contentpane.add(r13button,cons);      d2 = new jlabel("d2");     d2.settooltiptext("click button below reserve seat");     r14button = new jbutton("reserve d2");                  cons.gridx = 100;     cons.gridy = 7;     contentpane.add(d2,cons);     cons.gridx = 100;     cons.gridy = 8;     contentpane.add(r14button,cons);      e2 = new jlabel("e2");     e2.settooltiptext("click button below reserve seat");     r15button = new jbutton("reserve e2");                  cons.gridx = 100;     cons.gridy = 9;     contentpane.add(e2,cons);     cons.gridx = 100;     cons.gridy = 10;     contentpane.add(r15button,cons);      f2 = new jlabel("f2");     f2.settooltiptext("click button below reserve seat");     r16button = new jbutton("reserve f2");                  cons.gridx = 100;     cons.gridy = 11;     contentpane.add(f2,cons);     cons.gridx = 100;     cons.gridy = 12;     contentpane.add(r16button,cons);      g2 = new jlabel("g2");     g2.settooltiptext("click button below reserve seat");     r17button = new jbutton("reserve g2");                  cons.gridx = 100;     cons.gridy = 13;     contentpane.add(g2,cons);     cons.gridx = 100;     cons.gridy = 14;     contentpane.add(r17button,cons);      h2 = new jlabel("h2");     h2.settooltiptext("click button below reserve seat");     r18button = new jbutton("reserve h2");                  cons.gridx = 100;     cons.gridy = 15;     contentpane.add(h2,cons);     cons.gridx = 100;     cons.gridy = 16;     contentpane.add(r18button,cons);      i2 = new jlabel("i2");     i2.settooltiptext("click button below reserve seat");     r19button = new jbutton("reserve i2");                  cons.gridx = 100;     cons.gridy = 17;     contentpane.add(i2,cons);     cons.gridx = 100;     cons.gridy = 18;     contentpane.add(r19button,cons);      j2 = new jlabel("j2");     j2.settooltiptext("click button below reserve seat");     r20button = new jbutton("reserve j2");                  cons.gridx = 100;     cons.gridy = 19;     contentpane.add(j2,cons);     cons.gridx = 100;     cons.gridy = 20;     contentpane.add(r20button,cons);       //counter     counterlab = new jlabel("counter");     counter = new jtextfield(20);     counter.seteditable(false);     cons.gridx = 300;     cons.gridy = 5;     contentpane.add(counterlab,cons);     cons.gridx = 300;     cons.gridy = 6;     contentpane.add(counter,cons);      blabel = new jlabel("bill");     blabel.settooltiptext("enter bill below");     bill = new jtextfield(20);      billbutton = new jbutton("calculate");     cons.gridx = 300;     cons.gridy = 7;     contentpane.add(blabel,cons);     cons.gridx = 300;     cons.gridy = 8;     contentpane.add(bill,cons);     cons.gridx = 300;     cons.gridy = 9;     contentpane.add(billbutton,cons);      proghandler handler = new proghandler();      r1button.addactionlistener(handler);    r11button.addactionlistener(handler);     r2button.addactionlistener(handler);    r12button.addactionlistener(handler);     r3button.addactionlistener(handler);    r13button.addactionlistener(handler);     r4button.addactionlistener(handler);    r14button.addactionlistener(handler);     r5button.addactionlistener(handler);    r15button.addactionlistener(handler);     r6button.addactionlistener(handler);    r16button.addactionlistener(handler);     r7button.addactionlistener(handler);    r17button.addactionlistener(handler);     r8button.addactionlistener(handler);    r18button.addactionlistener(handler);         r9button.addactionlistener(handler);    r19button.addactionlistener(handler);         r10button.addactionlistener(handler);   r20button.addactionlistener(handler);      bill.addactionlistener(handler);     billbutton.addactionlistener(handler);     //counter.addactionlistener(handler);       }      private class proghandler implements actionlistener{      @override     public void actionperformed(actionevent e){        int price = 450;       int suc_price = 450;       int total;       int accumulator=1;            if(e.getsource()==r1button)       {        total = price * accumulator;          counter.settext("" +total);       r1button.setenabled(false);       accumulator++;         }       else if(e.getsource()==r2button)       {       total = price * accumulator;         counter.settext("" +accumulator);       r2button.setenabled(false);       accumulator++;         }      else if(e.getsource()==r3button)       {        counter.settext("" +price);       r3button.setenabled(false);       accumulator++;            }      else if(e.getsource()==r4button)       {        counter.settext("" +price);       r4button.setenabled(false);         }       else if(e.getsource()==r5button)       {        counter.settext("" +price);       r5button.setenabled(false);         }      else if(e.getsource()==r6button)       {        counter.settext("" +price);       r6button.setenabled(false);         }      else if(e.getsource()==r7button)       {        counter.settext("" +price);       r7button.setenabled(false);         }      else if(e.getsource()==r8button)       {        counter.settext("" +price);       r8button.setenabled(false);         }      else if(e.getsource()==r9button)       {        counter.settext("" +price);       r9button.setenabled(false);         }      else if(e.getsource()==r10button)       {        counter.settext("" +price);       r10button.setenabled(false);         }      else if(e.getsource()==r11button)       {        counter.settext("" +price);       r11button.setenabled(false);         }      else if(e.getsource()==r12button)       {        counter.settext("" +price);       r12button.setenabled(false);         }      else if(e.getsource()==r13button)       {        counter.settext("" +price);       r13button.setenabled(false);         }      else if(e.getsource()==r14button)       {        counter.settext("" +price);       r14button.setenabled(false);         }      else if(e.getsource()==r15button)       {        counter.settext("" +price);       r15button.setenabled(false);         }      else if(e.getsource()==r16button)       {        counter.settext("" +price);       r16button.setenabled(false);         }      else if(e.getsource()==r17button)       {        counter.settext("" +price);       r17button.setenabled(false);         }      else if(e.getsource()==r18button)       {        counter.settext("" +price);       r18button.setenabled(false);         }      else if(e.getsource()==r19button)       {        counter.settext("" +price);       r19button.setenabled(false);         }       else if(e.getsource()==r20button)       {        counter.settext("" +price);       r20button.setenabled(false);         }      }  }        } 

  • use jtogglebuttons reservation cinema, plain or bus

  • define setselectedicon() or change setbackground() on event jtogglebutton.isselected()

  • then there 2 ways (how determine 1 isselected())

    1. put jtogglebuttons in map or list

    2. loop inside contianer jtogglebuttons placed


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -