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
supportactionlisteners
Comments
Post a Comment