Advertisement
makispaiktis

8. JRadioButton using swing

May 17th, 2022
926
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.logging.Handler;
  4. import javax.swing.*;
  5.  
  6. public class Gui extends JFrame{
  7.  
  8.     // Variables
  9.     private JTextField tf;
  10.     private Font pf;
  11.     private Font bf;
  12.     private Font itf;
  13.     private Font bitf;
  14.     private JRadioButton pb;
  15.     private JRadioButton bb;
  16.     private JRadioButton itb;
  17.     private JRadioButton bitb;
  18.     private ButtonGroup group;       // Establishes a relationship, so that only 1 is selected
  19.  
  20.     // Constructor
  21.     public Gui(){
  22.  
  23.         // Basics
  24.         super("Title");
  25.         setLayout(new FlowLayout());
  26.  
  27.         // Text field
  28.         tf = new JTextField("Hello world!", 20);
  29.         add(tf);
  30.         // Only 1 radio button is initialized to 'true' at the beginning
  31.         pb = new JRadioButton("Plain", true);
  32.         bb = new JRadioButton("Bold", false);
  33.         itb = new JRadioButton("Italic", false);
  34.         bitb = new JRadioButton("Bold + Italic", false);
  35.         add(pb);
  36.         add(itb);
  37.         add(bb);
  38.         add(bitb);
  39.         // Group - This means that if I select 1, all THE OTHES WILL BE DESELECTED AUTOMATICALLY
  40.         group = new ButtonGroup();
  41.         group.add(pb);
  42.         group.add(bb);
  43.         group.add(itb);
  44.         group.add(bitb);
  45.         // Font
  46.         pf = new Font("Seirf", Font.PLAIN, 14);
  47.         bf = new Font("Seirf", Font.BOLD, 14);
  48.         itf = new Font("Seirf", Font.ITALIC, 14);
  49.         bitf = new Font("Seirf", Font.BOLD + Font.ITALIC, 14);
  50.         // Initialize the default font
  51.         tf.setFont(pf);
  52.  
  53.         // Listeners - Wait for event to happern, pass in font object to constructor
  54.         pb.addItemListener(new HandlerClass(pf));
  55.         bb.addItemListener(new HandlerClass(bf));
  56.         itb.addItemListener(new HandlerClass(itf));
  57.         bitb.addItemListener(new HandlerClass(bitf));
  58.     }
  59.  
  60.     private class HandlerClass implements ItemListener{
  61.  
  62.         //  Variable
  63.         private Font font;
  64.  
  65.         // Constructor
  66.         public HandlerClass(Font f){
  67.             font = f;
  68.         }
  69.  
  70.         // Method
  71.         public void itemStateChanged(ItemEvent event){
  72.             tf.setFont(font);
  73.         }
  74.  
  75.     }
  76.  
  77.  
  78.     // Main
  79.     public static void main(String[] args){
  80.         Gui gui = new Gui();
  81.         gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
  82.         gui.setSize(600, 600);
  83.         gui.setVisible(true);
  84.     }
  85.  
  86.  
  87. }
  88.  
Advertisement
RAW Paste Data Copied
Advertisement