Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Font;
- import java.awt.FlowLayout;
- import java.awt.event.ItemListener;
- import java.awt.event.ItemEvent;
- import javax.swing.JCheckBox;
- import javax.swing.JTextField;
- import javax.swing.JFrame;
- class CheckBox {
- public static void main(String args[ ]) {
- Box window = new Box();
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- window.setSize(250, 100);
- window.setVisible(true);
- }
- }
- class Box extends JFrame {
- private JTextField msg;
- private JCheckBox boldBox, italicBox;
- Box() {
- super("Font changer");
- setLayout(new FlowLayout());
- msg = new JTextField("This is my sentence.", 13);
- msg.setFont(new Font("Serif", Font.PLAIN, 14));
- add(msg);
- boldBox = new JCheckBox("Set bold");
- add(boldBox);
- italicBox = new JCheckBox("Set italic");
- add(italicBox);
- TheHandler handler = new TheHandler();
- boldBox.addItemListener(handler);
- italicBox.addItemListener(handler);
- }
- private class TheHandler implements ItemListener {
- private Font font;
- public void itemStateChanged(ItemEvent event) {
- if(boldBox.isSelected() && italicBox.isSelected())
- font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
- else if(boldBox.isSelected())
- font = new Font("Serif", Font.BOLD, 14);
- else if(italicBox.isSelected())
- font = new Font("Serif", Font.ITALIC, 14);
- else
- font = new Font("Serif", Font.PLAIN, 14);
- msg.setFont(font);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement