A basic implementation of JRadioButton class for showing radio buttons to set text as italic , bold , plain or italic + bold
Main class
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
radioButton guiB = new radioButton();
guiB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set size and visibility of window
guiB.setSize(300, 100);
guiB.setVisible(true);
}
}
Radio button class
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class radioButton extends JFrame {
private JTextField tf;
//radio buttons for plain , bold , italic and italic+bold fonts
private JRadioButton pR;
private JRadioButton bR;
private JRadioButton iR;
private JRadioButton biR;
private Font plain;
private Font bold;
private Font italic;
private Font boldItalic;
//faamily of button group to relate with eachother , when any radio button is pressed
private ButtonGroup group;
// constructor
public radioButton(){
//title of window
super("radio button");
//set layout
setLayout(new FlowLayout());
//initialise text field
tf = new JTextField("had a eve walk thru outskirts of village" , 25);
add(tf);
//initialise radio buttons
//@params - string as label and boolean value to know if button is checked by default
pR = new JRadioButton("plain" , true );
bR = new JRadioButton("bold" , false );
iR = new JRadioButton("italics" , false );
biR = new JRadioButton("bold n italics" , false );
//add buttons to window
add(pR); add(bR); add(iR); add(biR);
group = new ButtonGroup();
// add radio buttons to group
group.add(pR);
group.add(bR);
group.add(iR);
group.add(biR);
//initialise fonts
plain = new Font("Serif",Font.PLAIN , 30);
bold = new Font("Serif",Font.BOLD , 30);
italic = new Font("Serif",Font.ITALIC , 30);
boldItalic = new Font("Serif",Font.ITALIC + Font.BOLD , 30);
//set default font to textfield
tf.setFont(plain);
//add itemListener
//wait for event to happen and pass in font object to constructor
pR.addItemListener(new HandlerClass(plain));
bR.addItemListener(new HandlerClass(bold));
iR.addItemListener(new HandlerClass(italic));
biR.addItemListener(new HandlerClass(boldItalic));
}
private class HandlerClass implements ItemListener{
private Font font;
//sets the font to the font that was passed to contructor
@Override
public void itemStateChanged(ItemEvent e) {
tf.setFont(font);
}
public HandlerClass(Font f){
this.font = f ;
}
}
}
Main class
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
radioButton guiB = new radioButton();
guiB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set size and visibility of window
guiB.setSize(300, 100);
guiB.setVisible(true);
}
}
Radio button class
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class radioButton extends JFrame {
private JTextField tf;
//radio buttons for plain , bold , italic and italic+bold fonts
private JRadioButton pR;
private JRadioButton bR;
private JRadioButton iR;
private JRadioButton biR;
private Font plain;
private Font bold;
private Font italic;
private Font boldItalic;
//faamily of button group to relate with eachother , when any radio button is pressed
private ButtonGroup group;
// constructor
public radioButton(){
//title of window
super("radio button");
//set layout
setLayout(new FlowLayout());
//initialise text field
tf = new JTextField("had a eve walk thru outskirts of village" , 25);
add(tf);
//initialise radio buttons
//@params - string as label and boolean value to know if button is checked by default
pR = new JRadioButton("plain" , true );
bR = new JRadioButton("bold" , false );
iR = new JRadioButton("italics" , false );
biR = new JRadioButton("bold n italics" , false );
//add buttons to window
add(pR); add(bR); add(iR); add(biR);
group = new ButtonGroup();
// add radio buttons to group
group.add(pR);
group.add(bR);
group.add(iR);
group.add(biR);
//initialise fonts
plain = new Font("Serif",Font.PLAIN , 30);
bold = new Font("Serif",Font.BOLD , 30);
italic = new Font("Serif",Font.ITALIC , 30);
boldItalic = new Font("Serif",Font.ITALIC + Font.BOLD , 30);
//set default font to textfield
tf.setFont(plain);
//add itemListener
//wait for event to happen and pass in font object to constructor
pR.addItemListener(new HandlerClass(plain));
bR.addItemListener(new HandlerClass(bold));
iR.addItemListener(new HandlerClass(italic));
biR.addItemListener(new HandlerClass(boldItalic));
}
private class HandlerClass implements ItemListener{
private Font font;
//sets the font to the font that was passed to contructor
@Override
public void itemStateChanged(ItemEvent e) {
tf.setFont(font);
}
public HandlerClass(Font f){
this.font = f ;
}
}
}
No comments:
Post a Comment