java - Multiple Jbutton and ActionListener using inner class -


there 2 buttons app. 1 randomly changing color when clicked , other changing label in app.

the problem although i've write sepreate actionlistener classes , registered them correspondence methods respectively.

every time click "change label" button, color changes well. what's going on here?

package my;     import java.awt.*;     import java.awt.event.*;     import java.util.arraylist;      import javax.swing.*;      public class simplegui3c {          jframe frame;         jlabel label;          public static void main(string[] args){             simplegui3c gui = new simplegui3c();             gui.go();         }          public void go(){             frame = new jframe();             frame.setdefaultcloseoperation(jframe.exit_on_close);              jbutton labelbutton = new jbutton("change label");             labelbutton.addactionlistener(new labellistener());              jbutton colorbutton = new jbutton("change circle");             colorbutton.addactionlistener(new colorlistener());              label = new jlabel("i'm label");              mydrawpanel drawpanel = new mydrawpanel();              frame.getcontentpane().add(borderlayout.south,colorbutton);             frame.getcontentpane().add(borderlayout.center,drawpanel);             frame.getcontentpane().add(borderlayout.east, labelbutton);             frame.getcontentpane().add(borderlayout.west, label);              frame.setsize(300,300);             frame.setvisible(true);           }          class labellistener implements actionlistener{             public void actionperformed(actionevent event1) {                 arraylist<string> labelcontent = new arraylist<string>();                 labelcontent.add("ouch!");                 labelcontent.add("damn!");                 labelcontent.add("holy shit!");                 labelcontent.add("wtf?!");                 labelcontent.add("stop it!");                 labelcontent.trimtosize();                  int = (int)(math.random()*5);                 string = (string)labelcontent.get(i);                 label.settext(a);             }         }          class colorlistener implements actionlistener{             public void actionperformed(actionevent event2) {                 frame.repaint();             }         }     }              package my;             import java.awt.*;             import javax.swing.*;              public class mydrawpanel extends jpanel{                 /**                  *                   */                 private static final long serialversionuid = 1l;                  public void paintcomponent(graphics g){                     graphics2d g2d = (graphics2d) g;                      int red = (int)(math.random()*255);                     int blue = (int)(math.random()*255);                     int green = (int)(math.random()*255);                     color startcolor = new color(red,blue,green);                      red = (int)(math.random()*255);                     blue = (int)(math.random()*255);                     green = (int)(math.random()*255);                     color endcolor = new color(red,blue,green);                      int startpositionx = (int)(math.random()*70);                     int startpositiony = (int)(math.random()*70);                      int endpositionx = (int)(math.random()*150);                     int endpositiony = (int)(math.random()*150);                      gradientpaint gradient = new gradientpaint(startpositionx,startpositiony,startcolor,endpositionx,endpositiony,endcolor);                     g2d.setpaint(gradient);                     g2d.filloval(20,60,100,100);                  }             } 

each time click labelbutton firing repaint event mydrawpanel.

this causes color randomly generated again, changing color of mydrawpanel.

i suggest fixing calling method colorlistener on mydrawpanel changes color. keep panel changing color every repaint.

and here's precisely how it:

class colorlistener implements actionlistener{     mydrawpanel colorpanel;     public colorlistener(mydrawpanel panel){         this.colorpanel = panel;     }      public void actionperformed(actionevent event2) {         colorpanel.generaterandomcolor();         frame.repaint();     } }  public class mydrawpanel extends jpanel{     gradientpaint gradient;     public mydrawpanel(){          generaterandomcolor();     }     public void generaterandomcolor(){         int red = (int)(math.random()*255);         int blue = (int)(math.random()*255);         int green = (int)(math.random()*255);         color startcolor = new color(red,blue,green);          red = (int)(math.random()*255);         blue = (int)(math.random()*255);         green = (int)(math.random()*255);         color endcolor = new color(red,blue,green);          int startpositionx = (int)(math.random()*70);         int startpositiony = (int)(math.random()*70);          int endpositionx = (int)(math.random()*150);         int endpositiony = (int)(math.random()*150);          gradient = new gradientpaint(startpositionx,startpositiony,             startcolor, endpositionx,endpositiony,endcolor);      }  } 

Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -