Hi, I'm working on a program with GUI and I'm having a fair amount of trouble with the following code. As you can see below, I have an application and a support class.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StyleOptionsPanel extends JPanel
{
private JLabel saying;
private JCheckBox bold, italic;
private JRadioButton courier, timesnr, helvetica;
private int style = Font.PLAIN;
private String typeFace = "Helvetica";
//-----------------------------------------------------------------
// Sets up a panel with a label and some check boxes that
// control the style of the label's font.
//-----------------------------------------------------------------
public StyleOptionsPanel()
{
saying = new JLabel ("Say it with style!");
saying.setFont (new Font (typeFace, style, 20));
bold = new JCheckBox ("Bold");
bold.setBackground (Color.cyan);
italic = new JCheckBox ("Italic");
italic.setBackground (Color.cyan);
JRadioButton courier = new JRadioButton ("Courier");
courier.setFont (new Font ("Courier", style, 10));
JRadioButton timesnr = new JRadioButton ("Times New Roman");
timesnr.setFont (new Font ("Times New Roman", style, 10));
JRadioButton helvetica = new JRadioButton ("Helvetica");
helvetica.setFont (new Font (typeFace, style, 10));
StyleListener listener = new StyleListener();
bold.addItemListener (listener);
italic.addItemListener (listener);
courier.addItemListener (listener);
timesnr.addItemListener (listener);
helvetica.addItemListener (listener);
add (saying);
add (bold);
add (italic);
add (courier);
add (timesnr);
add (helvetica);
setBackground (Color.cyan);
setPreferredSize (new Dimension(300, 100));
}
//*****************************************************************
// Represents the listener for both check boxes.
//*****************************************************************
private class StyleListener implements ItemListener
{
//--------------------------------------------------------------
// Updates the style of the label font style.
//--------------------------------------------------------------
public void itemStateChanged (ItemEvent event)
{
style = Font.PLAIN;
if (bold.isSelected())
style = Font.BOLD;
if (italic.isSelected())
style += Font.ITALIC;
saying.setFont (new Font (typeFace, style, 20));
}
}
}
And the application class:
import javax.swing.JFrame;
public class StyleOptions
{
//-----------------------------------------------------------------
// Creates and presents the program frame.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Style Options");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
StyleOptionsPanel panel = new StyleOptionsPanel();
frame.getContentPane().add (panel);
frame.pack();
frame.setVisible(true);
}
}
The aim of what I am trying to do is add three functioning JRadioButtons which will offer three different typefaces (I have added them but can't seem to get them functioning). And then use a GridLayout to put all the items on the panel in a single column.
Any help would be very much greatly appreciated!
Thanks :)