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