Java AWT setBackground Color by ActionEvent -


i change backgroundcolor of containing frame, not seem work. added debug- messages , checked console output, switch working , setting background mainframe.setbackground() method.

    import java.awt.*;     import java.awt.event.*;  public class statewindow {     private frame mainframe;     private int bgcolor;       statewindow() {         graphicsdevice gd = graphicsenvironment.getlocalgraphicsenvironment().getdefaultscreendevice();         int scrwidth = gd.getdisplaymode().getwidth();         int scrheight = gd.getdisplaymode().getheight();         mainframe = new frame("statewindow");         mainframe.setsize(200, 200);         mainframe.setlayout(new borderlayout());         mainframe.setlocation((scrwidth-250), (scrheight-450));         mainframe.addwindowlistener(new windowadapter() {             public void windowclosing(windowevent windowevent) {                 system.exit(0);             }         });         bgcolor = 1;           panel centerpanel = new panel(new flowlayout());         label titlelabel = new label("statewindow", label.center);         button changebut = new button("change state");         changebut.setsize(60, 30);         centerpanel.add(changebut);          mainframe.add(titlelabel, borderlayout.north);         mainframe.add(centerpanel, borderlayout.center);         mainframe.setbackground(color.blue);          changebut.addactionlistener(new actionlistener() {              @override             public void actionperformed(actionevent e) {                  switch(bgcolor) {                     case 1: mainframe.setbackground(color.green); mainframe.repaint(); bgcolor = 2; break;                     case 2: mainframe.setbackground(color.orange); mainframe.repaint(); bgcolor = 3; break;                     case 3: mainframe.setbackground(color.red);  mainframe.repaint(); bgcolor = 1; break;                 }             }         });          mainframe.setvisible(true);     }      public static void main(string args[]) {         statewindow statewindow = new statewindow();      } } 

use panel , add frame , change background of panel. please check below code. make sure add child components panel.

    import java.awt.*;     import java.awt.event.*;      public class statewindow {         private frame mainframe;         private int bgcolor;          statewindow() {             graphicsdevice gd = graphicsenvironment.getlocalgraphicsenvironment()                     .getdefaultscreendevice();             int scrwidth = gd.getdisplaymode().getwidth();             int scrheight = gd.getdisplaymode().getheight();             mainframe = new frame("statewindow");             mainframe.setsize(200, 200);             mainframe.setlayout(new borderlayout());             mainframe.setlocation((scrwidth - 250), (scrheight - 450));             mainframe.addwindowlistener(new windowadapter() {                 public void windowclosing(windowevent windowevent) {                     system.exit(0);                 }             });             bgcolor = 1;              panel basepanel = new panel();             mainframe.add(basepanel);             panel centerpanel = new panel(new flowlayout());             label titlelabel = new label("statewindow", label.center);             button changebut = new button("change state");             changebut.setsize(60, 30);             centerpanel.add(changebut);              basepanel.add(titlelabel, borderlayout.north);             basepanel.add(centerpanel, borderlayout.center);             basepanel.setbackground(color.blue);              changebut.addactionlistener(new actionlistener() {                  @override                 public void actionperformed(actionevent e) {                      switch (bgcolor) {                     case 1:                         mainframe.getcomponent(0).setbackground(color.green);                         mainframe.repaint();                         bgcolor = 2;                         break;                     case 2:                         mainframe.getcomponent(0).setbackground(color.orange);                         mainframe.repaint();                         bgcolor = 3;                         break;                     case 3:                         mainframe.getcomponent(0).setbackground(color.red);                         mainframe.repaint();                         bgcolor = 1;                         break;                     }                 }             });              mainframe.setvisible(true);         }          public static void main(string args[]) {             statewindow statewindow = new statewindow();          }     } 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -