java - Need to make my texfield output bold, italic and both when the JCheckButtons are selected -


i have created basic gui has 2 jcheckbuttons (bold, italic) , 3jradiobuttons in button group of font styles (times, helvetica, courier). there textfield display string of font style, such times, in style, , if bold or italic buttons pressed text bold or italic (or both). far have managed add action listeners copy font name radio buttons textfield having trouble making text in associated font style. having trouble functions of bold , italic buttons.

here code gui layout (the layout how want be, problem functionality of buttons:

package weektwo; import javax.swing.*; import java.awt.*; import java.awt.gridbagconstraints; import java.awt.event.actionevent; import java.awt.event.actionlistener;  public class tasktwo {  public static void main(string[] args) {     jframe window = new jframe("font chooser");     window.setdefaultcloseoperation(jframe.exit_on_close);     window.setsize(500, 100);      fontsetter fontsetter = new fontsetter();      container pane = window.getcontentpane();     pane.setlayout(new gridbaglayout());      gridbagconstraints gbc = new gridbagconstraints();      jtextfield text = new jtextfield();     gbc.gridx = 2;     gbc.gridy = 2;     gbc.weightx = 1;     gbc.weighty = 1;     gbc.insets = new insets(0, 0, 5, 5); //padding constraints     gbc.fill = gridbagconstraints.both;   //text field fills cell     pane.add(text, gbc);      jcheckbox bold = new jcheckbox("bold");     gbc.gridx = 0; //sets x position 0     gbc.gridy = 1;     bold.addactionlistener(new bold(fontsetter, text));     pane.add(bold, gbc);      jcheckbox italic = new jcheckbox("italic");     gbc.gridx = 0;     gbc.gridy = 3;     italic.addactionlistener(new italic(fontsetter, text));     pane.add(italic, gbc);      jradiobutton times = new jradiobutton("times", true);     gbc.gridx = 1;     gbc.gridy = 0;     times.addactionlistener(new times(fontsetter, text));     pane.add(times, gbc);      jradiobutton helvetica = new jradiobutton("helvetica", false);     gbc.gridx = 1;     gbc.gridy = 2;     helvetica.addactionlistener(new helvetica(fontsetter, text));     pane.add(helvetica, gbc);      jradiobutton courier = new jradiobutton("courier", false);     gbc.gridx = 1;     gbc.gridy = 4;     courier.addactionlistener(new courier(fontsetter, text));     pane.add(courier, gbc);      buttongroup fonts = new buttongroup();  //now 1 of these buttons can selected @ once     fonts.add(times);     fonts.add(helvetica);     fonts.add(courier);      jbutton ok = new jbutton("ok");     gbc.gridx = 3;     gbc.gridy = 2;     //ok.addactionlistener();     pane.add(ok, gbc);      window.setvisible(true); } } 

and here classes action listeners:

class bold implements actionlistener {  private final fontsetter fontsetter; private final jtextfield textfield;  bold(fontsetter fontsetter, jtextfield textfield) {     this.fontsetter = fontsetter;     this.textfield = textfield; }  font font;  public void actionperformed(actionevent e) {     textfield.setfont(font.derivefont(font.bold)); }  }  class italic implements actionlistener {  private final fontsetter fontsetter; private final jtextfield textfield;  italic(fontsetter fontsetter, jtextfield textfield) {     this.fontsetter = fontsetter;     this.textfield = textfield; }  public void actionperformed(actionevent e) {     textfield.settext(""); }  }  class bolditalic implements actionlistener {  private final fontsetter fontsetter; private final jtextfield textfield;  bolditalic(fontsetter fontsetter, jtextfield textfield) {     this.fontsetter = fontsetter;     this.textfield = textfield; }  public void actionperformed(actionevent e) {     textfield.settext(""); }  }  class times implements actionlistener {  private final fontsetter fontsetter; private final jtextfield textfield;  times(fontsetter fontsetter, jtextfield textfield) {     this.fontsetter = fontsetter;     this.textfield = textfield; }  public void actionperformed(actionevent e) {     textfield.settext("times"); }  }  class helvetica implements actionlistener {  private final fontsetter fontsetter; private final jtextfield textfield;  helvetica(fontsetter fontsetter, jtextfield textfield) {     this.fontsetter = fontsetter;     this.textfield = textfield; }  public void actionperformed(actionevent e) {     textfield.settext("helvetica"); }  }  class courier implements actionlistener {  private final fontsetter fontsetter; private final jtextfield textfield;  courier(fontsetter fontsetter, jtextfield textfield) {     this.fontsetter = fontsetter;     this.textfield = textfield; }  public void actionperformed(actionevent e) {     textfield.settext("courier"); }  } 

i hope have explained , appreciate code, thanks.

p.s fontsetter class doesnt have function atm how intended layout action listeners

class fontsetter {  void setbold() {  }  void setitalic() {  }  void setbolditalic() {  }  void settimes() {  }  void sethelvetica() {  }  void setcourier() {  } } 

you can define variables in fontsetter class keep current font name , style

something this

class fontsetter {  private final jtextfield text; private string fontname = "times"; private boolean isbold = false; private boolean isitalic = false;  fontsetter(jtextfield text) {     this.text = text; }  void setbold(boolean flag) {     isbold = flag;     applyfontchanges(); }  void setitalic(boolean flag) {     isitalic = flag;     applyfontchanges(); }  void setfontname(string fontname) {     this.fontname = fontname;     applyfontchanges(); }  private void applyfontchanges(){     font font;     int style;      if(isbold && isitalic){         style = font.bold + font.italic;     } else if(isbold){         style = font.bold;     } else if(isitalic){         style = font.italic;     } else {         style = font.plain;     }      font = new font(fontname, style, 12);     text.setfont(font ); }    } 

then can use in actionlisteners change font this

fontsetter.setfontname("courier"); textfield.settext("courier"); 

and can change style follow

jcheckbox ch = (jcheckbox) e.getsource(); fontsetter.setbold(ch.isselected()); 

--

here complete code snippet


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -