/**
* @(#)ColorFontDemo.java
*
*
* @author - anant mahale
* @version 1.00 2013/3/18
*/
import java.awt.*;
import java.awt.event.*;
class ColorFont extends Frame implements ItemListener
{
private Label lbl_msg;
private Panel north_panel,south_panel,center_panel;
private Choice ch_forecolor,ch_backcolor,ch_size;
private Checkbox chb_bold,chb_italic,chb_plain;
private CheckboxGroup cg=new CheckboxGroup();
private int s;
public ColorFont()
{
north_panel=new Panel();
south_panel=new Panel();
center_panel=new Panel();
lbl_msg=new Label("Welcome to Java Programs");
ch_forecolor=new Choice();
ch_forecolor.add("Select Text Color");
ch_forecolor.add("Red");
ch_forecolor.add("Green");
ch_forecolor.add("Blue");
north_panel.add(ch_forecolor);
ch_forecolor.addItemListener(this);
ch_backcolor=new Choice();
ch_backcolor.add("Select Back color");
ch_backcolor.add("Red");
ch_backcolor.add("green");
ch_backcolor.add("blue");
ch_backcolor.addItemListener(this);
north_panel.add(ch_backcolor);
ch_size=new Choice();
ch_size.add("5");
ch_size.add("10");
ch_size.add("15");
ch_size.add("20");
ch_size.add("25");
ch_size.add("30");
ch_size.add("35");
ch_size.addItemListener(this);
add(north_panel,"North");
add(south_panel,"South");
add(center_panel);
chb_bold=new Checkbox("Bold");
chb_plain=new Checkbox("Plain",cg,true);
chb_italic=new Checkbox("Italic",cg,false);
chb_bold.addItemListener(this);
chb_plain.addItemListener(this);
chb_italic.addItemListener(this);
south_panel.add(chb_bold);
south_panel.add(chb_plain);
south_panel.add(chb_italic);
south_panel.add(ch_size);
center_panel.add(lbl_msg);
setTitle("ColorFont");
setSize(500,500);
setLocation(100,100);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==ch_forecolor)
{
int i=ch_forecolor.getSelectedIndex();
Color c=getColor(i);
lbl_msg.setForeground(c);
}
else if(ie.getSource()==ch_backcolor)
{
int i=ch_backcolor.getSelectedIndex();
Color c=getColor(i);
north_panel.setBackground(c);
south_panel.setBackground(c);
center_panel.setBackground(c);
}
else if(ie.getSource()==ch_size)
{
String msg=ch_size.getSelectedItem();
s=Integer.parseInt(msg);
if(chb_bold.getState()&&chb_plain.getState())
lbl_msg.setFont(new Font("ABC",Font.BOLD|Font.PLAIN,s));
if(chb_bold.getState()&&chb_italic.getState())
lbl_msg.setFont(new Font("ABC",Font.BOLD|Font.ITALIC,s));
else
lbl_msg.setFont(new Font("ABC",Font.ITALIC,s));
}
else if(ie.getSource()==chb_italic)
{
lbl_msg.setFont(new Font("ABC",Font.BOLD|Font.ITALIC,s));
}
else if(ie.getSource()==chb_bold)
{
if(chb_bold.getState())
lbl_msg.setFont(new Font("ABC",Font.BOLD,s));
else
lbl_msg.setFont(new Font("ABC",Font.PLAIN,s));
}
}
public static Color getColor(int i)
{
Color c=Color.white;
switch(i)
{
case 1:c=Color.red;break;
case 2:c=Color.green;break;
case 3:c=Color.blue;break;
}
return c;
}
}
class ColorFontDemo
{
public static void main(String args[])
{
new ColorFont().setVisible(true);
}
}
No comments:
Post a Comment