Monday, May 21, 2012

FontTest using JFrame with Item Listener!


   Here is another JFrame project using Listener methods. This program will prompt the user to select fonts of text and format. For questions please leave a message here. Some programmers understand a program by just reading its variable names that is why, by doing their jobs easier, programmers uses variable names which they can easily understand.

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

public class SampleApplet extends JFrame{
     JTextField lalagyanNgInfo;
     JCheckBox italic,bold;
     public Applet()
    {
        super("Item Listener (FONTS)");
        Container c=getContentPane();
        c.setLayout(new FlowLayout());
     
        lalagyanNgInfo=new JTextField ("palit font",20);
        lalagyanNgInfo.setFont(new Font("TimesRoman",Font.PLAIN,14));
        c.add(lalagyanNgInfo);
     
        //create Checkbox
        bold=new JCheckBox("bold");
        c.add(bold);
        italic=new JCheckBox("italic");
        c.add(italic);
     
     
        CheckBoxHandler handler = new CheckBoxHandler();
        bold.addItemListener(handler);
        italic.addItemListener(handler);
     
     
    }//End of public Applet


    public static void main (String args[ ])
    {
        JCheckBox CheckBoxTest=new JCheckBox();
    }
    //ItemListener code
    private class CheckBoxHandler implements ItemListener{
        private int styleBold=Font.PLAIN;
        private int styleItalic=Font.PLAIN;
        public void itemStateChanged(ItemEvent e)
        {
            if (e.getSource( )==bold)
                if (e.getStateChange( )==ItemEvent. SELECTED )
                    styleBold=Font.BOLD;
            else
                    styleBold=Font.PLAIN;
            if (e.getSource( )==italic)
                if (e.getStateChange( )==ItemEvent.SELECTED)
                    styleItalic=Font.ITALIC;
            else
                    valItalic = Font.PLAIN;
            lalagynNgInfo.setFont(new Font("TimesRoman",styleBold + styleItalic,14));
            lalagyanNgInfo.repaint( );
        }
    }
}
     
 

No comments:

Post a Comment