Setting checkboxes using JCheckBox class to italicise or bold a text
Main Class
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
checkBox guiB = new checkBox();
guiB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set size and visibility of window
guiB.setSize(300, 100);
guiB.setVisible(true);
}
}
Check box class
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.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class checkBox extends JFrame {
private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;
private Font font;
// constructor
public checkBox() {
super("title");
setLayout(new FlowLayout());
tf = new JTextField("Jingle all the way");
// set the font name , style and size
tf.setFont(new Font("Serif", Font.ROMAN_BASELINE, 40));
add(tf);
// initialise checkbox references
boldbox = new JCheckBox("bold");
italicbox = new JCheckBox("italics");
add(boldbox);
add(italicbox);
// create item event listener object
// When an item-selection event occurs, the listener object's
// itemStateChanged method is invoked.
Handler handle = new Handler();
boldbox.addItemListener(handle);
italicbox.addItemListener(handle);
}
private class Handler implements ItemListener {
@Override
public void itemStateChanged(ItemEvent event) {
// if bold and italic box is checked , change text to bold and
// italics
if (boldbox.isSelected() && italicbox.isSelected())
font = new Font("Serif", Font.BOLD + Font.ITALIC, 35);
// if bold box is selected
else if (boldbox.isSelected())
font = new Font("Serif", Font.BOLD, 35);
else if (italicbox.isSelected())
font = new Font("Serif", Font.ITALIC, 35);
else
// if nothing is selected , keep font plain
font = new Font("Serif", Font.PLAIN, 35);
// set the font for text field
tf.setFont(font);
}
}
}
Main Class
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
checkBox guiB = new checkBox();
guiB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set size and visibility of window
guiB.setSize(300, 100);
guiB.setVisible(true);
}
}
Check box class
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.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class checkBox extends JFrame {
private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;
private Font font;
// constructor
public checkBox() {
super("title");
setLayout(new FlowLayout());
tf = new JTextField("Jingle all the way");
// set the font name , style and size
tf.setFont(new Font("Serif", Font.ROMAN_BASELINE, 40));
add(tf);
// initialise checkbox references
boldbox = new JCheckBox("bold");
italicbox = new JCheckBox("italics");
add(boldbox);
add(italicbox);
// create item event listener object
// When an item-selection event occurs, the listener object's
// itemStateChanged method is invoked.
Handler handle = new Handler();
boldbox.addItemListener(handle);
italicbox.addItemListener(handle);
}
private class Handler implements ItemListener {
@Override
public void itemStateChanged(ItemEvent event) {
// if bold and italic box is checked , change text to bold and
// italics
if (boldbox.isSelected() && italicbox.isSelected())
font = new Font("Serif", Font.BOLD + Font.ITALIC, 35);
// if bold box is selected
else if (boldbox.isSelected())
font = new Font("Serif", Font.BOLD, 35);
else if (italicbox.isSelected())
font = new Font("Serif", Font.ITALIC, 35);
else
// if nothing is selected , keep font plain
font = new Font("Serif", Font.PLAIN, 35);
// set the font for text field
tf.setFont(font);
}
}
}
No comments:
Post a Comment