java - How do I make the background color change? -


i working on program has 5 different color radio buttons, , when clicked, background should change corresponding color. background not changing. cannot life of me figure out wrong code. can out there me find problem? thank you! code follows:

public void actionperformed(actionevent e) {     if (blue.getstate()) f.setbackground(color.blue);     else if (red.getstate()) f.setbackground(color.red);     else if (yellow.getstate()) f.setbackground(color.yellow);     else if (pink.getstate()) f.setbackground(color.pink);     else if (gray.getstate()) f.setbackground(color.gray); } //end of actionperformed method  public void itemstatechanged(itemevent e) { } 

more you're using java.awt.checkbox components (from your earlier question) respond itemlisteners not actionlisteners. therefore move code itemstatechanged method

public void itemstatechanged(itemevent e) {      if (blue.getstate()) {         f.setbackground(color.blue);     } else if (red.getstate()) {         f.setbackground(color.red);     } else if (yellow.getstate()) {         f.setbackground(color.yelloe);     } else if (pink.getstate()) {         f.setbackground(color.pink);     } else if (gray.getstate()) {         f.setbackground(color.gray);     } } 
  • use braces delimit scope
  • notice use of newer uppercase color constants
  • awt old limited ui library compared newer lightweight swing feature rich. swing jcheckboxes support actionlisteners

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -