Advertisement
thenewboston

Java Programming Tutorial - 64, 65 - JCheckBox

Aug 22nd, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. import java.awt.Font;
  2. import java.awt.FlowLayout;
  3. import java.awt.event.ItemListener;
  4. import java.awt.event.ItemEvent;
  5. import javax.swing.JCheckBox;
  6. import javax.swing.JTextField;
  7. import javax.swing.JFrame;
  8.  
  9. class CheckBox {
  10.    public static void main(String args[ ]) {
  11.       Box window = new Box();
  12.       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.       window.setSize(250, 100);
  14.       window.setVisible(true);
  15.    }
  16. }
  17.  
  18. class Box extends JFrame {
  19.    private JTextField msg;
  20.    private JCheckBox boldBox, italicBox;
  21.    
  22.    Box() {
  23.       super("Font changer");
  24.       setLayout(new FlowLayout());
  25.      
  26.       msg = new JTextField("This is my sentence.", 13);
  27.       msg.setFont(new Font("Serif", Font.PLAIN, 14));
  28.       add(msg);
  29.      
  30.       boldBox = new JCheckBox("Set bold");
  31.       add(boldBox);
  32.       italicBox = new JCheckBox("Set italic");
  33.       add(italicBox);
  34.      
  35.       TheHandler handler = new TheHandler();
  36.       boldBox.addItemListener(handler);
  37.       italicBox.addItemListener(handler);
  38.      
  39.    }
  40.    
  41.    private class TheHandler implements ItemListener {
  42.       private Font font;
  43.      
  44.       public void itemStateChanged(ItemEvent event) {
  45.          if(boldBox.isSelected() && italicBox.isSelected())
  46.             font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
  47.          else if(boldBox.isSelected())
  48.             font = new Font("Serif", Font.BOLD, 14);
  49.          else if(italicBox.isSelected())
  50.             font = new Font("Serif", Font.ITALIC, 14);
  51.          else
  52.             font = new Font("Serif", Font.PLAIN, 14);
  53.          
  54.          msg.setFont(font);
  55.       }
  56.    }
  57.    
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement