java:
write an applet to display the message Welcome to Java Programming
The program will have a checkbox to display the message in italic and or bold.
The program will have a combo box that uses a string array called fontNames to store the following font names, ["Dialog", "Century", "Gothic", "Courier", "Serif"]. Depending on the selection in the font name in combo box, the applet will display the message using the font name.
The program will have a radio button (Red, Green, Blue) to display the message depending on the color selection in the radio button. Note the radio button can only select one item at a time.
Here is the code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="FontApplet" height=600 width=800>
</applet>
*/
public class FontApplet extends Applet implements ItemListener
{
Label lbl1;
Choice cfontname;
Checkbox chk1, chk2, chk3, chk4, chk5;
CheckboxGroup cbgrp;
Font font1;
String fontNames[] = {
"Dialog",
"Century",
"Gothic",
"Courier",
"Serif"
};
public void init() {
setLayout(null);
//define the items
lbl1 = new Label("Welcome to Java Programming");
cbgrp = new CheckboxGroup();
cfontname = new Choice();
chk1 = new Checkbox("Bold");
chk2 = new Checkbox("Italics");
chk3 = new Checkbox("Red", cbgrp, false);
chk4 = new Checkbox("Green", cbgrp, false);
chk5 = new Checkbox("Blue", cbgrp, false);
// place the items
lbl1.setBounds(50, 40, 380, 30);
chk1.setBounds(60, 100, 100, 30);
chk2.setBounds(60, 160, 100, 30);
cfontname.setBounds(170, 100, 100, 30);
chk3.setBounds(310, 100, 100, 30);
chk4.setBounds(310, 130, 100, 30);
chk5.setBounds(310, 160, 100, 30);
//add all items
add(lbl1);
for (int i = 0; i < fontNames.length; i++) {
cfontname.add(fontNames[i]);
}
add(lbl1);
add(cfontname);
add(chk1);
add(chk3);
add(chk2);
add(chk5);
add(chk4);
add(chk5);
chk1.addItemListener(this);
chk2.addItemListener(this);
cfontname.addItemListener(this);
chk3.addItemListener(this);
chk4.addItemListener(this);
chk5.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie) {
String selected_font = "";
int font_type = 0;
if (chk1.getState() == true) {
font_type = Font.BOLD;
font1 = new Font("", Font.BOLD, 20);
//lbl1.setFont(font1);
}
if (chk2.getState() == true) {
font1 = new Font("", Font.ITALIC, 20);
font_type = Font.ITALIC;
//lbl1.setFont(font1);
}
if (chk3.getState() == true) {
lbl1.setForeground(Color.RED);
} else if (chk4.getState() == true) {
lbl1.setForeground(Color.GREEN);
} else if (chk5.getState() == true) {
lbl1.setForeground(Color.BLUE);
}
//set the font
selected_font = cfontname.getSelectedItem();
font1 = new Font(selected_font, font_type, 20);
lbl1.setFont(font1);
repaint();
}
}
Save above code in file FontApplet.java . Compile the file and
run from applet viewer as below
$ javac FontApplet.java
$ appletviewer FontApplet.java
The image of applet goes here
Get Answers For Free
Most questions answered within 1 hours.