Advertisement
Guest User

Untitled

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