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 Font pf;
- private Font bf;
- private Font itf;
- private Font bitf;
- private JRadioButton pb;
- private JRadioButton bb;
- private JRadioButton itb;
- private JRadioButton bitb;
- private ButtonGroup group; // Establishes a relationship, so that only 1 is selected
- // Constructor
- public Gui(){
- // Basics
- super("Title");
- setLayout(new FlowLayout());
- // Text field
- tf = new JTextField("Hello world!", 20);
- add(tf);
- // Only 1 radio button is initialized to 'true' at the beginning
- pb = new JRadioButton("Plain", true);
- bb = new JRadioButton("Bold", false);
- itb = new JRadioButton("Italic", false);
- bitb = new JRadioButton("Bold + Italic", false);
- add(pb);
- add(itb);
- add(bb);
- add(bitb);
- // Group - This means that if I select 1, all THE OTHES WILL BE DESELECTED AUTOMATICALLY
- group = new ButtonGroup();
- group.add(pb);
- group.add(bb);
- group.add(itb);
- group.add(bitb);
- // Font
- pf = new Font("Seirf", Font.PLAIN, 14);
- bf = new Font("Seirf", Font.BOLD, 14);
- itf = new Font("Seirf", Font.ITALIC, 14);
- bitf = new Font("Seirf", Font.BOLD + Font.ITALIC, 14);
- // Initialize the default font
- tf.setFont(pf);
- // Listeners - Wait for event to happern, pass in font object to constructor
- pb.addItemListener(new HandlerClass(pf));
- bb.addItemListener(new HandlerClass(bf));
- itb.addItemListener(new HandlerClass(itf));
- bitb.addItemListener(new HandlerClass(bitf));
- }
- private class HandlerClass implements ItemListener{
- // Variable
- private Font font;
- // Constructor
- public HandlerClass(Font f){
- font = f;
- }
- // Method
- public void itemStateChanged(ItemEvent event){
- 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