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 1.65 KB | None | 0 0
  1. import java.awt.FlowLayout;
  2. import java.awt.Font;
  3. import java.awt.event.ItemListener;
  4. import java.awt.event.ItemEvent;
  5. import javax.swing.JFrame;
  6. import javax.swing.JTextField;
  7. import javax.swing.JCheckBox;
  8. public class CheckBoxFrame extends JFrame{
  9. private JTextField textField;
  10. private JCheckBox boldJCheckBox;
  11. private JCheckBox italicJCheckBox;
  12.  
  13. public CheckBoxFrame(){
  14. super("JCheckBox Test");
  15. setLayout(new FlowLayout());
  16.  
  17. textField = new JTextField("Watch the font style change",20);
  18. textField.setFont(new Font("Serif",Font.PLAIN , 14));
  19. add(textField);
  20.  
  21. boldJCheckBox = new JCheckBox ("Bold");
  22. italicJCheckBox = new JCheckBox("Italic");
  23. add(italicJCheckBox);
  24. add(boldJCheckBox);
  25.  
  26. CheckBoxHandler handler = new CheckBoxHandler();
  27. boldJCheckBox.addItemListener((ItemListener) handler);
  28. italicJCheckBox.addItemListener((ItemListener) handler);
  29.  
  30. }
  31. private class CheckBoxHandler implements ItemListener {
  32. public void itemStateChanged( ItemEvent event ){
  33. Font font = null;
  34. if (boldJCheckBox.isSelected() && italicJCheckBox.isSelected())
  35. font = new Font ( "Serif ", Font.BOLD + Font.ITALIC, 14);
  36. else if (boldJCheckBox.isSelected())
  37. font = new Font("Serif", Font.BOLD, 14);
  38. else if (italicJCheckBox.isSelected())
  39. font = new Font ("Serif", Font.ITALIC, 14);
  40. else
  41. font = new Font ("Serif", Font.PLAIN, 14);
  42.  
  43. textField.setFont(font);
  44. }
  45.  
  46.  
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement