Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. package checkboxtest;
  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.JCheckBox;
  9.  
  10. public class CheckBoxFrame extends JFrame {
  11. private JTextField textField;
  12. private JCheckBox boldJCheckBox;
  13. private JCheckBox italicJCheckBox;
  14. public CheckBoxFrame(){
  15. super( "JCheckBox Test");
  16. setLayout( new FlowLayout());
  17. textField = new JTextField( "Watch the font style change", 20 );
  18. textField.setFont( new Font( "Serif", Font.PLAIN, 14));
  19. add( textField );
  20. boldJCheckBox = new JCheckBox("Bold");
  21. italicJCheckBox = new JCheckBox("Italic");
  22. add( boldJCheckBox);
  23. add( italicJCheckBox);
  24. CheckBoxHandler handler = new CheckBoxHandler();
  25. boldJCheckBox.addItemListener( handler );
  26. italicJCheckBox.addItemListener( handler );
  27. }
  28. private class CheckBoxHandler implements ItemListener{
  29. public void itemStateChanged( ItemEvent event ){
  30. Font font = null;
  31. if (boldJCheckBox.isSelected() && italicJCheckBox.isSelected())
  32. font = new Font( "Serif", Font.BOLD + Font.ITALIC, 14) ;
  33. else if (boldJCheckBox.isSelected())
  34. font = new Font( "serif", Font.BOLD, 14);
  35. else if (italicJCheckBox.isSelected())
  36. font = new Font("Serif", Font.ITALIC, 14);
  37. else
  38. font = new Font( "Serif", Font.PLAIN, 14);
  39. textField.setFont( font );
  40.  
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement