Java AWT Example - Font - Internet & Web Programming Lab

Program:

import java.awt.*;
import java.awt.event.*;

class font extends Frame implements ItemListener
{
 Choice c1,c2,c3;
 Label lb;
 int a,b;
 String c;
 font()
 {
   setLayout(null);
   setSize(500,400);
   setVisible(true);
   setTitle("Java Awt : Font");
   c1=new Choice();
   c2=new Choice();
   c3=new Choice();
   lb=new Label(null);
   lb.setBackground(Color.white);
   lb.setForeground(Color.blue);
   lb.setAlignment(1);
   c1.setBounds(50,100,100,20);
   add(c1);
   c2.setBounds(160,100,100,20);
   add(c2);
   c3.setBounds(270,100,100,20);
   add(c3);
   lb.setBounds(10,200,480,60);
   add(lb);
   c1.addItemListener(this);
   c2.addItemListener(this);
   c3.addItemListener(this);
   lb.setText("www.2k8618.blogspot.com");
   c1.add("Normal");
   c1.add("Bold");
   c1.add("Italic");
   for(int i=8;i<72;i+=2)
     c2.add(Integer.toString(i));
     GraphicsEnvironment
     ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
     String f[]=ge.getAvailableFontFamilyNames();
     for(int i=0;i<f.length;i++)
        c3.add(f[i]);

   addWindowListener(new WindowAdapter()
   {
    public void windowClosing(WindowEvent we)
    {
     System.exit(0);
    }
   });
 }

 public void itemStateChanged(ItemEvent ae)
 {
  if(c1==ae.getSource())
  {
        if(c1.getSelectedIndex()==0)
          a=Font.PLAIN;
        else if(c1.getSelectedIndex()==1)
          a=Font.BOLD;
        else if(c1.getSelectedIndex()==2)
          a=Font.ITALIC;
        lb.setFont(new Font(c,a,b));
        lb.setText(lb.getText());
  }
  if(c2==ae.getSource())
  {
    b=Integer.parseInt(c2.getSelectedItem());
    lb.setFont(new Font(c,a,b));
    lb.setText(lb.getText());
  }
  if(c3==ae.getSource())
  {
    c=c3.getSelectedItem();
    lb.setFont(new Font(c,a,b));
    lb.setText(lb.getText());
  }
 }

 public static void main(String s[])
 {
  font ob=new font();
 }
}


Output:

nn@linuxmint ~/Desktop/java_all $ javac font.java
nn@linuxmint ~/Desktop/java_all $ java font
nn@linuxmint ~/Desktop/java_all $



1 comment:

Related Posts Plugin for WordPress, Blogger...