Advertisement
Guest User

Untitled

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