rendered paste bodyimport javax.swing.*;
import java.awt.event.*;
import java.awt.Container; //need this to add controls
import java.awt.*; //need this for layoutmanager
public class StylingWithTextSize extends JFrame
{
private JFrame mainFrame;
private JButton exitButton;
private JLabel inLabel;
private JTextField tinField;
private JCheckBox boxItalic;
private JCheckBox boxBold;
private JRadioButton rdbutCourier;
private JRadioButton rdbutSansSerif;
private JRadioButton rdbutSerif;
private JLabel textLabel;
private JTextField fontField;
public StylingWithTextSize() // a constructor
{
mainFrame = new JFrame("Check Box Example");
//create all components
exitButton = new JButton("Exit");
inLabel = new JLabel("Enter Some Text:");
tinField = new JTextField(20);
boxItalic = new JCheckBox("Italic");
boxBold = new JCheckBox("Bold");
rdbutCourier = new JRadioButton("COurier");
rdbutSansSerif = new JRadioButton("SansSerif");
rdbutSerif = new JRadioButton("Serif");
textLabel = new JLabel("Font Size");
fontField = new JTextField(2);
//group radio buttons
ButtonGroup rgroup = new ButtonGroup();
rgroup.add(rdbutCourier);
rgroup.add(rdbutSansSerif);
rgroup.add(rdbutSerif);
// get the content pane
Container c = mainFrame.getContentPane();
// set the ;aypit ,amager
c.setLayout(new FlowLayout());
//add the components to the ContentPane
c.add(inLabel);
c.add(tinField);
c.add(boxItalic);
c.add(boxBold);
c.add(exitButton);
c.add(rdbutCourier);
c.add(rdbutSansSerif);
c.add(rdbutSerif);
c.add(fontField);
//create accelerator keys
boxItalic.setMnemonic('I');
boxBold.setMnemonic('B');
exitButton.setMnemonic('x');
rdbutCourier.setMnemonic('C');
rdbutSansSerif.setMnemonic('S');
rdbutSerif.setMnemonic('f');
mainFrame.setSize(300,150);
//set the initial button and corresponding font
rdbutCourier.setSelected(true);
tinField.setFont(new Font("Courier", Font.PLAIN, 12));
//declration registration
ChkBoxHandler chandler = new ChkBoxHandler();
boxItalic.addItemListener(chandler);
boxBold.addItemListener(chandler);
rdbutCourier.addItemListener(chandler);
rdbutSansSerif.addItemListener(chandler);
rdbutSerif.addItemListener(chandler);
//define and register window event handler
mainFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {System.exit(0);}
});
//create and register the exit button event handler
ExitButtonHandler ehandler = new ExitButtonHandler(); //instantiate a handler
exitButton.addActionListener(ehandler); //register the handler
mainFrame.show();
}
//Check Box listener class code
class ChkBoxHandler implements ItemListener
{
private int italicFont;
private int boldFont;
private int style;
private int size;
public ChkBoxHandler()
{
}
public void itemStateChanged(ItemEvent e)
{
/*if (e.getSource() == boxBold)
if (e.getStateChange() == e.SELECTED)
boldFont = Font.BOLD;
else
boldFont = Font.PLAIN;
else
if (e.getStateChange() == e.SELECTED)
italicFont = Font.ITALIC;
else
italicFont = Font.PLAIN;*/
style = Font.PLAIN;
if(boxItalic.isSelected()) style += Font.ITALIC;
if(boxBold.isSelected()) style += Font.BOLD;
tinField.setFont( new Font( "Courier", style, 12));
//rbutton stuff
if (e.getSource() == rdbutCourier)
tinField.setFont(new Font("Courier", Font.PLAIN, 12));
else if (e.getSource() == rdbutSansSerif)
tinField.setFont(new Font("SansSerif", Font.PLAIN, 12));
else if (e.getSource() == rdbutSerif)
tinField.setFont(new Font("Serif", Font.PLAIN, 12));
tinField.repaint();
}
}// end of check box listener class
public void focusGained (FocusEvent e){}
public void focusLost(FocusEvent e)
{
if(e.getSource() == fontField)
size = Integer.parseInt(fontField.getText());
}
//inner class for the Exit button event handler
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
} // end of inner class
public static void main(String args[])
{
new StylingWithTextSize(); // instantiate a GUI object
}
} //end of class