Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //********************************************************************
- // StyleOptionsPanel.java adapted from Java Foundations
- //
- // Demonstrates the use of check boxes.
- //********************************************************************
- 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, times, helv;
- private ButtonGroup fonts;
- 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");
- JRadioButton times = new JRadioButton("Times New Roman");
- JRadioButton helv = new JRadioButton("Helvetica");
- ButtonGroup fonts = new ButtonGroup();
- fonts.add(courier);
- fonts.add(times);
- fonts.add(helv);
- FontListener listener1 = new FontListener();
- courier.addActionListener(listener1);
- times.addActionListener(listener1);
- helv.addActionListener(listener1);
- StyleListener listener = new StyleListener();
- bold.addItemListener (listener);
- italic.addItemListener (listener);
- add(courier);
- add(times);
- add(helv);
- add (saying);
- add (bold);
- add (italic);
- 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));
- }
- }
- private class FontListener implements ActionListener{
- public void actionPerformed(ActionEvent event){
- Object source = event.getSource();
- typeFace = "Helvetica";
- if(source == courier) typeFace = "Courier";
- else if(source == times) typeFace = "Times New Roman";
- else typeFace = "Helvetica";
- saying.setFont(new Font(typeFace, style, 20));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement