Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. package checkbox;
  2.  
  3. import java.awt.FlowLayout;
  4. import java.awt.Font;
  5. import java.awt.event.ItemListener;
  6. import java.awt.event.ItemEvent;
  7. import javax.swing.JFrame;
  8. import javax.swing.JTextField;
  9. import javax.swing.JCheckBox;
  10.  
  11. public class CheckboxFrame extends JFrame{
  12.  
  13. private JTextField textField;
  14. private JCheckBox boldJCheckBox;
  15. private JCheckBox ItalicJCheckBox;
  16.  
  17. public CheckboxFrame(){
  18. super("JCheckBox Text");
  19. setLayout(new FlowLayout());
  20.  
  21. textField = new JTextField("Watch the font style change", 20);
  22. textField.setFont(new Font("Serif", Font.PLAIN, 14));
  23. add(textField);
  24.  
  25. boldJCheckBox = new JCheckBox("Bold");
  26. ItalicJCheckBox = new JCheckBox("Italic");
  27. add(boldJCheckBox);
  28. add(ItalicJCheckBox);
  29.  
  30. CheckBoxHandler handler = new CheckBoxHandler();
  31. boldJCheckBox.addItemListener(handler);
  32. ItalicJCheckBox.addItemListener(handler);
  33. }
  34. private class CheckBoxHandler implements ItemListener{
  35. public void itemStateChanged(ItemEvent event){
  36. Font font= null;
  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.  
  42. }else if(ItalicJCheckBox.isSelected()){
  43. font= new Font("Serif", Font.ITALIC, 14);
  44. }else {
  45. font = new Font("Serif", Font.PLAIN, 14);
  46. }
  47. textField.setFont(font);
  48. }
  49. }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement