Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.util.logging.Handler;
- import javax.swing.*;
- public class Gui extends JFrame{
- // Variables
- private JTextField tf;
- private JCheckBox boldbox;
- private JCheckBox italicbox;
- // Constructor
- public Gui(){
- // Basics
- super("JFrame's title");
- setLayout(new FlowLayout());
- // Create the objects - variables
- tf = new JTextField("This is a sentence", 20);
- tf.setFont(new Font("Serif", Font.PLAIN, 14));
- add(tf);
- boldbox = new JCheckBox("Bold");
- italicbox = new JCheckBox("Italic");
- add(boldbox);
- add(italicbox);
- // Handler things in !!!! ItemListener !!!!
- HandlerClass handler = new HandlerClass();
- boldbox.addItemListener(handler);
- italicbox.addItemListener(handler);
- }
- private class HandlerClass implements ItemListener{
- public void itemStateChanged(ItemEvent event){
- Font font = null;
- 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);
- }
- tf.setFont(font);
- }
- }
- // Main
- public static void main(String[] args){
- Gui gui = new Gui();
- gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
- gui.setSize(600, 600);
- gui.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement