Question

java: write an applet to display the message Welcome to Java Programming The program will have...

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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
B.2. Write a Java program to display a dialog box that asks you to enter your...
B.2. Write a Java program to display a dialog box that asks you to enter your user name and the age as shown below: The program displays in a dialog box the sum of digits of your age. For example, if you age is 19, the sum will be 1+9 = 10 and the output will be as shown below: ( it just shows a message box that says the sum of the digits age of john is 10.
Objective: Write a Java program that will use a JComboBox from which the user will select...
Objective: Write a Java program that will use a JComboBox from which the user will select to convert a temperature from either Celsius to Fahrenheit, or Fahrenheit to Celsius. The user will enter a temperature in a text field from which the conversion calculation will be made. The converted temperature will be displayed in an uneditable text field with an appropriate label. Specifications Structure your file name and class name on the following pattern: The first three letters of your...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you start doing anything… This project involves implementing a simple university personnel management program. The program contains two different kinds of objects: students and faculty. For each object, the program stores relevant information such as university ID, name, etc. Different information is stored depending on the type of the object. For example, a student has a GPA, while a faculty has a title and department...