Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. package radiobuttontest;
  2. import java.awt.FlowLayout;
  3. import java.awt.Font;
  4. import java.awt.event.ItemListener;
  5. import java.awt.event.ItemEvent;
  6. import javax.swing.JFrame;
  7. import javax.swing.JTextField;
  8. import javax.swing.JRadioButton;
  9. import javax.swing.ButtonGroup;
  10. class RadioButtonFrame extends JFrame{
  11. private JTextField textField;
  12. private Font plaintFont;
  13. private Font boldFont;
  14. private Font boldItalicFonte;
  15. private JRadioButton plainJRadioButton;
  16. private JRadioButton boldJRadioButton;
  17. private JRadioButton ItalicJRadioButton;
  18. private JRadioButton boldItalicJRadioButton;
  19. private ButtonGroup radioGroup;
  20. private final Font plainFont;
  21. private final Font italicFont;
  22. private final Font boldItalicFont;
  23. public RadioButtonFrame(){
  24. super ("RadioButton Test");
  25. setLayout (new FlowLayout());
  26. textField = new JTextField("Watch the font style change", 25);
  27. add (textField);
  28.  
  29. plainJRadioButton = new JRadioButton ("Plain", true);
  30. boldJRadioButton = new JRadioButton ("Bold", false);
  31. ItalicJRadioButton = new JRadioButton ("Italic", false);
  32. boldItalicJRadioButton = new JRadioButton ("Bold/Italic", false);
  33. add (plainJRadioButton);
  34. add (boldJRadioButton);
  35. add (ItalicJRadioButton);
  36. add (boldItalicJRadioButton);
  37.  
  38. radioGroup = new ButtonGroup();
  39. radioGroup.add (plainJRadioButton);
  40. radioGroup.add (boldJRadioButton);
  41. radioGroup.add (ItalicJRadioButton);
  42. radioGroup.add (boldItalicJRadioButton);
  43.  
  44. plainFont = new Font ("Serif", Font.PLAIN, 14);
  45. boldFont = new Font ("Serif", Font.BOLD, 14);
  46. italicFont = new Font ("Serif", Font.ITALIC, 14);
  47. boldItalicFont = new Font ("Serif", Font.BOLD + Font.ITALIC, 14);
  48. textField.setFont (plainFont);
  49.  
  50. plainJRadioButton.addItemListener(
  51. new RadioButtonHandler (plainFont));
  52. boldJRadioButton.addItemListener(
  53. new RadioButtonHandler (boldFont));
  54. ItalicJRadioButton.addItemListener
  55. ( new RadioButtonHandler (italicFont));
  56. boldItalicJRadioButton.addItemListener(
  57. new RadioButtonHandler (boldItalicFont));
  58. }
  59. private class RadioButtonHandler implements ItemListener {
  60. private Font font;
  61.  
  62. public RadioButtonHandler ( Font F){
  63. font = F;
  64. }
  65. public void itemStateChanged (ItemEvent event){
  66. textField.setFont ( font );
  67. }
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement