Results 1 to 3 of 3
  1. #1
    Addis is offline Member
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Default I nee help .....I did it but it doesn't work like BOLD,ITALIC and CENTER

    (Selecting a font) Write a program that can dynamically change the font of a
    message to be displayed on a panel. The message can be displayed in bold and
    italic at the same time, or can be displayed in the center of the panel. You can select
    the font name or font size from combo boxes, as shown in Figure 17.39. The available
    font names can be obtained using getAvailableFontFamilyNames() in
    GraphicsEnvironment (§12.8, “The Font Class”). The combo box for font size
    is initialized with numbers from 1 to 100.


    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Font;
    import java.awt.GraphicsEnvironment;
    import java.awt.event.*;

    import javax.swing.*;
    import javax.swing.border.*;

    //import SelectAFont.Listener;

    public class SelectAFont extends JFrame{

    private Jlabel jlblMessage = new Jlabel("Java is Cool");

    // Create check boxes to set font message
    private JCheckBox jchkCentered = new JCheckBox("Centered");
    private JCheckBox jchkBold = new JCheckBox("Bold");
    private JCheckBox jchkItalic = new JCheckBox("Italic");

    //Create Combo boxes to select font type and size

    private JComboBox<String> jcboFontName = new JComboBox<String>();
    private JComboBox jcboFontSize = new JComboBox ();



    private String fontName = "SansSerif";

    private int fontStyle = Font.PLAIN;

    // Font Size
    private int fontSize = 18;

    // Declare a panel for displaying message




    /** Main method */
    public static void main(String[] args) {
    SelectAFont frame = new SelectAFont();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setTitle("Exercise 17.8");
    frame.pack();
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setVisible(true);
    }


    public class Jlabel {

    public Jlabel(String string) {
    // TODO Auto-generated constructor stub
    }



    public Font setFont(Font font) {
    return font;
    // TODO Auto-generated method stub

    }

    public void setAlignmentX(float centerAlignment) {



    }

    }


    /** Default constructor */
    public SelectAFont() {


    JPanel p1 = new JPanel(new BorderLayout(10, 10));
    JLabel jlblMessage = new JLabel("Java is Cool");
    p1.add(jlblMessage, BorderLayout.CENTER);


    // Create a Panel to hold Label and Combo box

    JPanel pA = new JPanel(new BorderLayout(10, 10));
    JLabel JFontN = new JLabel("Font Name");
    pA.add((JFontN), BorderLayout.WEST);
    pA.add(jcboFontName, BorderLayout.CENTER);


    //Create a panel to hold font size label and combo box
    JPanel pB = new JPanel(new BorderLayout(10, 10));
    JLabel JFontS = new JLabel("Font Size");
    pB.add((JFontS), BorderLayout.WEST);
    pB.add(jcboFontSize, BorderLayout.CENTER);

    // Add pA and pB into pC
    JPanel pC = new JPanel(new BorderLayout(10, 10));
    pC.setBorder(new EmptyBorder(10, 10, 10, 10));
    pC.add(pA, BorderLayout.WEST);
    pC.add(pB, BorderLayout.EAST);


    // Put three check boxes in panel p
    JPanel p = new JPanel();
    p.add(jchkCentered = new JCheckBox("Centered"));
    p.add(jchkBold = new JCheckBox("Bold"));
    p.add(jchkItalic = new JCheckBox("Italic"));


    // Set keyboard mnemonics
    jchkCentered.setMnemonic('C');
    jchkBold.setMnemonic('B');
    jchkItalic.setMnemonic('I');


    // Place jlblMessage, pC, and p in the frame
    setLayout(new BorderLayout());
    add(p1, BorderLayout.CENTER);
    add(pC, BorderLayout.NORTH);
    add(p, BorderLayout.SOUTH);




    // Set the background color of messagePanel


    // Find all available font names
    GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String[] fontnames = e.getAvailableFontFamilyNames();
    for (int i = 0; i < fontnames.length; i++)
    jcboFontName.addItem(fontnames[i]);
    jcboFontName.setSelectedItem("" + fontName);

    // Add font sizes into jcboFontSize
    for (int i = 1; i <= 100; i++)
    jcboFontSize.addItem("" + i);
    jcboFontSize.setSelectedItem("" + fontSize);

    // Register listeners on jcboFontName and jcboFontSize
    jcboFontName.addItemListener(new Listener());
    jcboFontSize.addItemListener(new Listener());


    // Register listeners on jchkCentered, jchkBold, and jchkItalic

    jchkCentered.addItemListener(new Listener());
    jchkBold.addItemListener(new Listener());
    jchkItalic.addItemListener(new Listener());
    }



    //private void add(Jlabel jlblMessage2, String center) {
    // TODO Auto-generated method stub

    // }

    class Listener implements ItemListener {
    /** Handle check box selection */
    public void itemStateChanged(ItemEvent e) {
    if (e.getSource() == jcboFontName) {
    fontName = (String)(jcboFontName.getSelectedItem());

    // Set font for the message
    jlblMessage.setFont(new Font(fontName, fontStyle, fontSize));
    }
    else if (e.getSource() == jcboFontSize) {
    fontSize = Integer.parseInt(
    (String)(jcboFontSize.getSelectedItem()));

    // Set font for the message
    jlblMessage.setFont(new Font(fontName, fontStyle, fontSize));
    }

    // Register listeners with check boxes
    else if (e.getSource() == jchkBold){
    jchkBold.addItemListener(new ItemListener(){
    @Override
    public void itemStateChanged(ItemEvent e) {
    setNewFont();
    }


    });}
    else if (e.getSource() == jchkItalic){
    jchkItalic.addItemListener(new ItemListener(){
    @Override
    public void itemStateChanged(ItemEvent e) {
    setNewFont();
    }


    });}


    else if (e.getSource() == jchkCentered){
    jchkCentered.addItemListener(new ItemListener(){
    public void itemStateChanged(ItemEvent e) {

    if (e.getSource() == jchkCentered) {
    if (jchkCentered.isSelected())
    jlblMessage.setAlignmentX(CENTER_ALIGNMENT);
    else
    jlblMessage.setAlignmentX(EXIT_ON_CLOSE);
    }
    }
    });}


    }


    private void setNewFont() {
    // Determine a font style
    int fontStyle = Font.PLAIN;
    fontStyle += (jchkBold.isSelected()? Font.BOLD:Font.PLAIN);
    fontStyle += (jchkItalic.isSelected()? Font.ITALIC:Font.PLAIN);

    // Set font for Message
    Font font =jlblMessage.setFont(getFont());
    jlblMessage.setFont(new Font(font.getFontName(),fontStyle,font.getSize())) ;

    }


    }


    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: I nee help .....I did it but it doesn't work like BOLD,ITALIC and CENTER

    Please go through the Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: I nee help .....I did it but it doesn't work like BOLD,ITALIC and CENTER

    And while you're about it, get rid of inane and incorrect comments. They add clutter and hamper readability.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. I wonder why this doesn't work...
    By louboulos in forum New To Java
    Replies: 3
    Last Post: 05-13-2012, 04:59 PM
  2. Jar doesn't work
    By mad72584 in forum New To Java
    Replies: 35
    Last Post: 08-07-2011, 05:22 PM
  3. why this doesn't work?
    By hitesh_public in forum New To Java
    Replies: 5
    Last Post: 08-09-2010, 08:07 AM
  4. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 10:33 PM
  5. Bold Italic function in Swing
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 02-19-2009, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •