|
Java Program To Demonstrate Awt Choice Control |
Algorithm Steps:
Step 1: Write a html applet tag with code property set to the class name and comment it
Step 2: Import necessary packages and classes
Step 3: Define a class that extends applet and implements item listener
Step 4: Declare a choice fonts
Step 5: In the init() method, do the following:
i) Declare the choice control
ii) Populate the control with various font names
iii) Add item listener to the choice
iv) add the fonts choice to the applet
Step 6: In the itemStateChanged method repaint the applet
Step 7: In the paint() method do the following:
i) Get the selected choice item
ii) Create a font of the selected item value
iii) Set the font of the graphics of the applet to the created font
iv) Display any text on the applet
Java Program To Demonstrate Awt Choice Control
/* */
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ChoiceApplet extends Applet implements ItemListener {
Choice fonts;
TextArea t;
Choice c;
public void init(){
fonts = new Choice();
c=new Choice();
c.add("yellow");
c.add("blue");
add(c);
t=new TextArea();
add(t);
fonts.add("Arial");
fonts.add("Arial Black");
fonts.add("Book Antiqua");
fonts.add("Bookman Old Style");
fonts.add("Garamond");
fonts.add("Symbol");
fonts.addItemListener(this);
c.addItemListener(this);
add(fonts);
}
public void itemStateChanged(ItemEvent ie){
repaint();
}
public void paint(Graphics g){
String name = fonts.getSelectedItem();
Font myFont = new Font(name,Font.PLAIN,30);
g.setFont(myFont);
g.drawString("The New Font Selected Is "+name, 100,300);
String str=c.getSelectedItem();
if(str.equals("yellow"))
{ t.setBackground(Color.yellow);}
else if(str.equals("blue"))
{t.setBackground(Color.blue);}
else{t.setBackground(Color.black);}
}
}
SAMPLE OUTPUT SCREEN:
|
|
⇓ Student Projects ⇓
⇑ Student Projects ⇑
|