Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 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.  
  11. public class RadioButtonFrame extends JFrame {
  12. private JTextField textField;
  13. private Font plainFont;
  14. private Font boldFont;
  15. private Font italicFont;
  16. private Font boldItalicFont;
  17. private JRadioButton plainJRadioButton;
  18. private JRadioButton boldJRadioButton;
  19. private JRadioButton italicJRadioButton;
  20. private JRadioButton boldItalicJRadioButton;
  21. private ButtonGroup radioGroup;
  22.  
  23. public RadioButtonFrame(){
  24. super("RadioButton Test");
  25. setLayout(new FlowLayout());
  26.  
  27. textField = new JTextField("Watch the font style change",25);
  28. add (textField);
  29.  
  30. plainJRadioButton = new JRadioButton("Plain", true);
  31. boldJRadioButton = new JRadioButton("bold",false);
  32. italicJRadioButton = new JRadioButton("Italic", false);
  33. boldItalicJRadioButton = new JRadioButton("Bold/Italic",false);
  34. add(plainJRadioButton);
  35. add(boldJRadioButton);
  36. add(italicJRadioButton);
  37. add(boldItalicJRadioButton);
  38.  
  39. radioGroup = new ButtonGroup();
  40. radioGroup.add(plainJRadioButton);
  41. radioGroup.add(boldJRadioButton);
  42. radioGroup.add(italicJRadioButton);
  43. radioGroup.add(boldItalicJRadioButton);
  44.  
  45. plainFont = new Font("Serif",Font.PLAIN,14);
  46. boldFont = new Font ("Serif",Font.BOLD,14);
  47. italicFont = new Font("Serif",Font.ITALIC,14);
  48. boldItalicFont = new Font ("Serif", Font.BOLD + Font.ITALIC,14);
  49. textField.setFont(plainFont);
  50.  
  51.  
  52. plainJRadioButton.addItemListener(
  53. new RadioButtonHandler(plainFont));
  54. boldJRadioButton.addItemListener(
  55. new RadioButtonHandler(boldFont));
  56. italicJRadioButton.addItemListener(
  57. new RadioButtonHandler(italicFont));
  58. boldItalicJRadioButton.addItemListener(
  59. new RadioButtonHandler(boldItalicFont));
  60.  
  61. }
  62.  
  63. private class RadioButtonHandler implements ItemListener{
  64. private Font font;
  65. public RadioButtonHandler(Font f){
  66. font = f;
  67. }
  68.  
  69. public void itemStateChanged(ItemEvent event){
  70. textField.setFont(font);
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement