java - JRadio Buttons and Font change -
i having little bit of trouble trying jradio buttons change color of text depending on player chooses. making gui game setup page. like, choose army, followed choose number of players want interact with.
here have:
package systemanddesign.risk; import java.awt.flowlayout; import java.awt.font; import java.awt.color; import java.awt.event.itemlistener; import java.awt.event.itemevent; import javax.swing.jtextfield; import javax.swing.jradiobutton; import javax.swing.jframe; import javax.swing.buttongroup; public class charactercreation extends jframe{ private font redfont; private font bluefont; private font greenfont; private font yellowfont; private font grayfont; private font blackfont; private jradiobutton three; private jradiobutton four; private jradiobutton five; private jradiobutton six; private jradiobutton red; private jradiobutton blue; private jradiobutton green; private jradiobutton yellow; private jradiobutton gray; private jradiobutton black; private jtextfield textfield; private buttongroup army; private buttongroup numplayers; public charactercreation(){ super("character creation"); setlayout(new flowlayout()); textfield = new jtextfield("your army color", 25); add(textfield); //to remain true game of risk, instead of having individual name input game. //i allowing army color player wants be. //when game done being written out, hope have font of individual player text, color //of army. //in future when model done, character screen prevent players having same color army. redfont = new font("serif", font.plain, 14); bluefont = new font("serif", font.plain,14); greenfont = new font("serif", font.plain,14); yellowfont = new font("serif", font.plain,14); grayfont = new font("serif", font.plain,14); blackfont = new font("serif", font.plain,14); textfield.setfont(redfont); red = new jradiobutton("red army", true); blue = new jradiobutton("blue army", false); green = new jradiobutton("green army", false); yellow = new jradiobutton("yellow army", false); gray = new jradiobutton("gray army", false); black = new jradiobutton("black army", false); add(red); add(blue); add(green); add(yellow); add(gray); add(black); army = new buttongroup(); army.add(red); army.add(blue); army.add(green); army.add(yellow); army.add(gray); army.add(black); red.additemlistener(new radiobuttonhandler(redfont)); blue.additemlistener(new radiobuttonhandler(bluefont)); green.additemlistener(new radiobuttonhandler(greenfont)); yellow.additemlistener(new radiobuttonhandler(yellowfont)); gray.additemlistener(new radiobuttonhandler(grayfont)); black.additemlistener(new radiobuttonhandler(blackfont)); //end of army selection radiobuttons. //now player selection radiobuttons. 3 = new jradiobutton("3 players", true); 4 = new jradiobutton("4 players", false); 5 = new jradiobutton("5 players", false); 6 = new jradiobutton("6 players", false); add(three); add(four); add(five); add(six); numplayers = new buttongroup(); numplayers.add(three); numplayers.add(four); numplayers.add(five); numplayers.add(six); } private class radiobuttonhandler implements itemlistener{ private font font; public radiobuttonhandler(font f){ font = f; } public void itemstatechanged(itemevent event){ textfield.setfont(font); if(event.equals(red)){ textfield.setbackground(color.red); } else if(event.equals(blue)){ textfield.setbackground(color.blue); } else if(event.equals(green)){ textfield.setbackground(color.green); } else if(event.equals(yellow)){ textfield.setbackground(color.yellow); } else if(event.equals(gray)){ textfield.setbackground(color.gray); } else if(event.equals(black)){ textfield.setbackground(color.black); } } } }
the background foreground alas, neither have worked. doing wrong?
in itemstatechanged()
should compare source of event , not event itself. example:
if(event.getsource().equals(red)){ textfield.setbackground(color.red); }
you can use actionlistener
on radio buttons. see how use radio buttons example.
Comments
Post a Comment