java - Issues with JButtons and ActionEvent -


for reason, when click buttons, makes actionperformed method, program can't match of buttons actionevent, nothing works when of buttons pressed. took code out of paintcomponent , put in constructor, buttons don't show up. can please help?

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;  //  public class game extends jframe//header {         private cardpanel mp;         private font big = new font("verdana", font.bold, 100);         private font small = new font("verdana", font.bold, 90);         private font norm = new font("verdana", font.plain, 23);         private cardlayout cards;         private jbutton back;         private boolean clairewang=false;         private boolean wangfam=false;         private boolean cl=false;         private jbutton next;         private jbutton instruction;         private jbutton startb;          private jbutton done;         private jtextarea title;         private color c=new color(186,225,200);         private int a=0;;         private jbutton yes;         private jbutton no;         private int x;         private int y;         private jbutton claire= new jbutton("play!");         private jbutton fambam=new jbutton("play!");         private jbutton sickbob=new jbutton("play!");         private string pc="play claire (medium)- able bodied 20 yearold";         private string pfb="play family (hard)- receive more waterper week.";         private string psb="play bob (easy)- connected thecity's water" ;        private string psb2="supply because has brain cancer.";            public static void main(string [] args)         {                 game b = new game();//create constructor         }         public game()//bio constructor         {            //set jframe            setsize(900,600);            setdefaultcloseoperation(dispose_on_close);            setlocation(0,0);            setresizable(false);//set false            mp = new cardpanel();            cards = new cardlayout();            = new jbutton("back");            done= new jbutton("done");            next = new jbutton("next");                                                 next.setbounds(275,200, 315, 220);              start beg = new start();             instruct1 inst1 = new instruct1();            instruct2 inst2 = new instruct2();            choosepanel ch = new choosepanel();            sb1 sb= new sb1();            fb1 f1= new fb1();//finish            fb2 f2= new fb2();            fb3 f3= new fb3();            fb4 f4= new fb4();            c1 s= new c1();             mp.setlayout(cards);            mp.add(beg,"start");             mp.add(inst1,"i1");            mp.add(inst2, "i2");            mp.add(ch,"choose");                         mp.add(s, "c1");                         mp.add(sb, "sb");                         mp.add(f1, "fb1");                         mp.add(f2, "fb2");                         mp.add(f3, "fb3");                         mp.add(f4, "fb4");            setcontentpane(mp);            setvisible(true);         }          class cardpanel extends jpanel         {                 public cardpanel()                 {                 }         }         class start extends jpanel implements actionlistener         {                  public start()                 {                     setlayout(null);                      startb = new jbutton("start");                     startb.setbounds(255,200, 315, 100);                      instruction = new jbutton("instructions");                     instruction.setbounds(255,320, 315, 100);                                                   //make text area uneditable                      instruction.addactionlistener(this);                     startb.addactionlistener(this);                      add(startb);                     add(instruction);                 }                 public void paintcomponent(graphics g)                 {                        setbackground(c);                                                 super.paintcomponent(g); //import image                                                 setfont(big);                                                 g.setcolor(color.white);                                                 g.drawstring("thirst", 230,150);                                         //      g.setcolor(c);                                         //      g.drawstring("thirst",235,153);                                         //      g.setcolor(color.white);//have loop draws waves                                  for( y=0;y<600;y+=200){                                         for(x=0;x<800;x+=100){                                                 g.drawarc(x,y, 100,50,0,-180);                                                 }                                 }                                         for(y=100;y<600;y+=200){                                         for(x=-50;x<800;x+=100){                                                 g.drawarc(x,y, 100,50,0,-180);                                                 }                                         }                                   //for( y=0; y<600;x+=100){                                 //              for(x=0; x<800;x+=50){                                         //                      g.drawarc(x, y, 50,50,0,-180);                                         //      }                                 //}                  }                  public void actionperformed(actionevent evt)                 {                                          if (evt.getsource()==instruction) {                                                          cards.show(mp,"i1"); }                                         else if(evt.getsource()==startb){                                                 cards.show(mp, "choose");                                         }                  }         }        /* class instruct extends jpanel implements actionlistener         {//header                  public instruct()                 {                                                 setlayout(null);                                                   next = new jbutton("next");                                                 next.setbounds(600,500,100,50);                                                 //instruction.setbounds(225,500, 275, 575);                         = new jbutton("back");                                                 back.setbounds(100,500,100,50);                      //   instruction.setbounds(225,500, 375, 575);                       next.addactionlistener(this);                       back.addactionlistener(this);                                                 add(next);                          add(back);                   }                 public void paintcomponent(graphics g){                         setbackground(c);                         super.paintcomponent(g);                         setfont(big);                                                 g.setcolor(color.white);                                                 g.drawstring("thing0", 230,150);                          //draw instructions in center using blackor white                  }                 public void actionperformed(actionevent evt){                         //  if(((jbutton)(evt.getsource()).gettext()).equals("back"))                                                  //       cards.show(mp,"start");                                                 //       system.out.println("x");                                                 //       }                                                 //if ((((jbutton)(evt.getsource()).gettext()).equals("next")) ){                                                          //cards.show(mp,"i1");                                                          //system.out.println("x");                                                         //}                                                          if (evt.getsource()==next ){                                                          cards.show(mp,"i1");                                                          system.out.println("x");                                                         }                                                         else if (evt.getsource()==back) {                                                  cards.show(mp,"start");                                                          system.out.println("x");                                                          }                    }                          } */                          class instruct1 extends jpanel implements actionlistener{                                 public instruct1(){                                         setlayout(null);                                                 next = new jbutton("next");                                                 next.setbounds(600,500,100,50);                                                 //instruction.setbounds(225,500, 275, 575);                         = new jbutton("back");                                                 back.setbounds(100,500,100,50);                                                 next.addactionlistener(this);                                                 back.addactionlistener(this);                                                 add(next);                                                 add(back);                                         }                                 public void paintcomponent(graphics g){                                                 setbackground(c);                                                 super.paintcomponent(g); //import image                                                 setfont(big);                                                 g.setcolor(color.white);                                                 g.drawstring("thing1", 230,150);                                         }                                                 public void actionperformed(actionevent evt){                          if (evt.getsource()==back) {                                                          cards.show(mp,"start"); }                                                 else if(evt.getsource()==next){                                                          cards.show(mp,"i2");                                                         }          }                                  }                         class instruct2 extends jpanel implements actionlistener{                                 public instruct2(){                                 setlayout(null);                                                   done = new jbutton("done");                                                 = new jbutton("back");                                                  back.addactionlistener(this);                                                 done.addactionlistener(this);                                                  done.setbounds(600,500,100,50);                                                 //instruction.setbounds(225,500, 275, 575);                                                  back.setbounds(100,500,100,50);                                                 add(done);                                                 add(back);                                          }                                 public void paintcomponent(graphics g){                                                  setbackground(c);                                                 super.paintcomponent(g); //import image                                                 setfont(big);                                                 g.setcolor(color.white);                                                 g.drawstring("thing2", 230,150);                                         }                                 public void actionperformed(actionevent evt){                                              if (evt.getsource()==back) {                                                 cards.show(mp,"i1"); }                                                 else if(evt.getsource()==done){                                                  cards.show(mp,"start");                                                         }          }         }         class choosepanel extends jpanel implements actionlistener{                         public choosepanel(){                                 setlayout(null);                                 sickbob.addactionlistener(this);                                 fambam.addactionlistener(this);                                 claire.addactionlistener(this);                                 sickbob.setbounds(600,200,100,50);                                 claire.setbounds(600,300,100,50);                                 fambam.setbounds(600,400,100,50);                                 add(sickbob);                                 add(claire);                                 add(fambam);                                                  //instruction.setbounds(225,500, 275, 575);                          }                         public void paintcomponent(graphics g){                                 setbackground(c);                                 super.paintcomponent(g);                         setfont(small);                                                 g.setcolor(color.white);                                                 g.drawstring("choose player", 0,150);                                 g.setfont(norm);                                 g.drawstring(psb,50,225);                                 g.drawstring(psb2,50,250);                                 g.drawstring(pc,50,325);                                 g.drawstring(pfb,50,425);                          }                         public void actionperformed(actionevent evt){                         if(evt.getsource()==claire){                                 cards.show(mp, "c1");                                 clairewang=true;                             }                          else if(evt.getsource()==sickbob){                                 cards.show(mp, "c1");                                 cl=true;}                         else if(evt.getsource()==fambam){                                 cards.show(mp, "c1");                                 wangfam=true;                         }                          }                       /*  public boolean stuff(){                             if(wangfam) return wangfam;                             else if(cl)return cl;                             else if(clairewang)return clairewang;                         } */           }         class c1 extends jpanel implements actionlistener{                 public void c1(){                     /* setlayout(null);                     next=new jbutton("next");                     no=new jbutton("no");                     yes=new jbutton("yes");                      yes.addactionlistener(this);                      no.addactionlistener(this);                     next.addactionlistener(this);                     yes.setbounds(100,200,100,50);                     no.setbounds(300,300,100,50);                     next.setbounds(700,400,100,50);                     add(yes);                     add(no);                     add(next); */                      next=new jbutton("next");                     no=new jbutton("no");                     yes=new jbutton("yes");                      yes.addactionlistener(this);                      no.addactionlistener(this);                     next.addactionlistener(this);                     yes.setbounds(100,200,100,50);                     no.setbounds(300,200,100,50);                     next.setbounds(700,500,100,50);                     add(yes);                     add(no);                     add(next);                   }                 public void paintcomponent(graphics g){                         setbackground(c);                                 super.paintcomponent(g);                                 setlayout(null);                          setfont(norm);                                                 g.setcolor(color.white);                                                 g.drawstring("your flowers wilting. water lawn?",20,100);                                                 //print situation yn                                                 //jbutton yes , j button no                                          g.fillrect(700, 75, 100, 400);//draw bar shows how wateris left                                         g.fillarc(700,65,100,20,0,180);                                         g.fillarc(700,465,100,20,0,-180);                                         g.setcolor(c);                                         g.fillrect(705,82, 90,390);                                         g.fillarc(705, 463, 90, 18, 0 , -180);                                         g.setcolor(color.white);                                         g.fillrect(705,82, 90,a);                                        // if(clairewang)system.out.println(a);                                          if(wangfam)system.out.println("wangfam");                                         else if(cl)system.out.println("cl");                    }                 public void actionperformed(actionevent evt){                     system.out.println("stuff");                         if(evt.getsource()==yes){                                 if(clairewang){                                     a=15;                                     system.out.println(a);}                                 else if(wangfam)a+=20;                                 else if(cl)a+=10;                                 //clairewang=true;                                 repaint();                              }                          else if(evt.getsource()==next){                                 cards.show(mp, "sb");                                // cl=true;}                          //yes gives int value                         //no stays 0                 }             }         }   } 

move creation , init code out of paintcomponent() constructor

the code

            next=new jbutton("next");             no=new jbutton("no");             yes=new jbutton("yes");              yes.addactionlistener(this);              no.addactionlistener(this);             next.addactionlistener(this);             yes.setbounds(100,200,100,50);             no.setbounds(300,200,100,50);             next.setbounds(700,500,100,50);             add(yes);             add(no);             add(next); 

in fact on each repaint recreates buttons , readd them. in actionperformed() button state changed (to show pressed state) , button recreated. , of course != clicked one.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -