Specify Font Size

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FLEMEF
    New Member
    • Mar 2007
    • 1

    #1

    Specify Font Size

    Hello All, I'm not very good at Java...

    With this piece of Java code, how could I modify the StyleOptions.ja va program to allow the user to specify the size of the font, while using the text field to obtain the user’s input.

    Thanks.
    -----------------------------------------------------------------

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event. *;

    public class StyleOptionsPan el extends JPanel
    {
    private JLabel saying;
    private JCheckBox bold, italic;


    public StyleOptionsPan el()
    {
    saying = new JLabel ("Say it with style!");
    saying.setFont (new Font ("Helvetica" , Font.PLAIN, 36));

    bold = new JCheckBox ("Bold");
    bold.setBackgro und (Color.cyan);
    italic = new JCheckBox ("Italic");
    italic.setBackg round (Color.cyan);


    StyleListener listener = new StyleListener() ;
    bold.addItemLis tener (listener);
    italic.addItemL istener (listener);

    add (saying);
    add (bold);
    add (italic);

    setBackground (Color.cyan);
    setPreferredSiz e (new Dimension(300, 100));
    }

    private class StyleListener implements ItemListener
    {
    //--------------------------------------------------------------
    // Updates the style of the label font style.
    //--------------------------------------------------------------
    public void itemStateChange d (ItemEvent event)
    {
    int style = Font.PLAIN;

    if (bold.isSelecte d())
    style = Font.BOLD;

    if (italic.isSelec ted())
    style += Font.ITALIC;

    saying.setFont (new Font ("Helvetica" , style, 36));
    }
    }
    }
    =============== =============== =============== ======

    import javax.swing.JFr ame;

    public class StyleOptions
    {

    public static void main (String[] args)
    {
    JFrame frame = new JFrame ("Style Options");
    frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);

    StyleOptionsPan el panel = new StyleOptionsPan el();
    frame.getConten tPane().add (panel);

    frame.pack();
    frame.setVisibl e(true);
    }
    }
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    Maybe I missed the point, but you could add a button (or combo box or other thing) with a new listener....

    Comment

    Working...