SHARE
TWEET

radio buttons text editor

a guest Sep 15th, 2019 115 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //********************************************************************
  2. //  StyleOptionsPanel.java      adapted from Java Foundations
  3. //
  4. //  Demonstrates the use of check boxes.
  5. //********************************************************************
  6.  
  7. import javax.swing.*;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10.  
  11. public class StyleOptionsPanel extends JPanel
  12. {
  13.    private JLabel saying;
  14.    private JCheckBox bold, italic;
  15.    private JRadioButton courier, times, helv;
  16.    private ButtonGroup fonts;
  17.    private int style = Font.PLAIN;
  18.    private String typeFace = "Helvetica";
  19.  
  20.    //-----------------------------------------------------------------
  21.    //  Sets up a panel with a label and some check boxes that
  22.    //  control the style of the label's font.
  23.    //-----------------------------------------------------------------
  24.    public StyleOptionsPanel()
  25.    {
  26.       saying = new JLabel ("Say it with style!");
  27.       saying.setFont (new Font (typeFace, style, 20));
  28.  
  29.       bold = new JCheckBox ("Bold");
  30.       bold.setBackground (Color.cyan);
  31.       italic = new JCheckBox ("Italic");
  32.       italic.setBackground (Color.cyan);
  33.  
  34.       JRadioButton courier = new JRadioButton("Courier");
  35.       JRadioButton times = new JRadioButton("Times New Roman");
  36.       JRadioButton helv = new JRadioButton("Helvetica");
  37.      
  38.       ButtonGroup fonts = new ButtonGroup();
  39.       fonts.add(courier);
  40.       fonts.add(times);
  41.       fonts.add(helv);
  42.      
  43.       FontListener listener1 = new FontListener();
  44.       courier.addActionListener(listener1);
  45.       times.addActionListener(listener1);
  46.       helv.addActionListener(listener1);
  47.      
  48.       StyleListener listener = new StyleListener();
  49.       bold.addItemListener (listener);
  50.       italic.addItemListener (listener);
  51.      
  52.       add(courier);
  53.       add(times);
  54.       add(helv);
  55.       add (saying);
  56.       add (bold);
  57.       add (italic);
  58.  
  59.       setBackground (Color.cyan);
  60.       setPreferredSize (new Dimension(300, 100));
  61.    }
  62.  
  63.    //*****************************************************************
  64.    //  Represents the listener for both check boxes.
  65.    //*****************************************************************
  66.    private class StyleListener implements ItemListener
  67.    {
  68.       //--------------------------------------------------------------
  69.       //  Updates the style of the label font style.
  70.       //--------------------------------------------------------------
  71.       public void itemStateChanged (ItemEvent event)
  72.       {
  73.          style = Font.PLAIN;
  74.  
  75.          if (bold.isSelected())
  76.             style = Font.BOLD;
  77.  
  78.          if (italic.isSelected())
  79.             style += Font.ITALIC;
  80.  
  81.          saying.setFont (new Font (typeFace, style, 20));
  82.       }
  83.    }
  84.    
  85.    private class FontListener implements ActionListener{
  86.      
  87.      public void actionPerformed(ActionEvent event){
  88.        
  89.       Object source = event.getSource();
  90.       typeFace = "Helvetica";
  91.        
  92.       if(source == courier)  typeFace = "Courier";
  93.      
  94.       else if(source == times) typeFace = "Times New Roman";
  95.      
  96.       else typeFace = "Helvetica";
  97.      
  98.       saying.setFont(new Font(typeFace, style, 20));
  99.      }
  100.    }
  101. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top