Friday, 22 April 2011

radio button on java

JAVA MAIN FILE
-----------------

package radio;

import javax.swing.JFrame;

public class Main
{
    public static void main(String[] args)
    {
        boss obj = new boss();
        obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        obj.setSize(300, 300);
        obj.setVisible(true);
    }
}

JAVA CLASS FILE
-------------------

package radio;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class boss extends JFrame
{
    private JTextField tf;
    private Font pf;
    private Font bf;
    private Font itf;
    private Font bif;
    private JRadioButton pb;
    private JRadioButton bb;
    private JRadioButton ib;
    private JRadioButton bib;
    private ButtonGroup group;

    public boss()
    {
    super("title");
    setLayout(new FlowLayout());

    tf = new JTextField("Try on me", 25);
    tf.setEditable(false);
    add(tf);

    pb = new JRadioButton("plain", true);
    bb = new JRadioButton("bold", false);
    ib = new JRadioButton("italic", false);
    bib = new JRadioButton("bold and italic", false);
    add(pb);
    add(bb);
    add(ib);
    add(bib);

    group = new ButtonGroup();
    group.add(pb);
    group.add(bb);
    group.add(ib);
    group.add(bib);

    pf = new Font("Serif", Font.PLAIN, 14);
    bf = new Font("Serif", Font.BOLD, 14);
    itf = new Font("Serif", Font.ITALIC, 14);
    bif = new Font("Serif", Font.BOLD + Font.ITALIC, 14);

    tf.setFont(pf);

    pb.addItemListener(new HandlerClass(pf));
    bb.addItemListener(new HandlerClass(bf));
    ib.addItemListener(new HandlerClass(itf));
    bib.addItemListener(new HandlerClass(bif));
    }

    private class HandlerClass implements ItemListener
    {
        public Font font;

        //set font object get variable font
        public HandlerClass(Font f)
        {
            font = f;
        }

        //sets the font to the font object that was passed in
        public void itemStateChanged(ItemEvent ie)
        {
            tf.setFont(font);
        }
    }
}

No comments:

Post a Comment