Advertisement
Guest User

Untitled

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