Back to Applets page
Click in the boxes below, select all and copy to grab the applet
HTML listing:
<HTML> <HEAD> <TITLE>Listing 8.6</TITLE> </HEAD> <BODY BGCOLOR=WHITE> <APPLET CODEBASE="classes" CODE="Applet6.class" WIDTH=500 HEIGHT=300> </APPLET> </BODY> </HTML>
Applet listing:
import java.awt.*; import java.applet.Applet; public class Applet6 extends Applet { TextField inputField; String displayText = "Enter a string to display"; String fontFace; Choice fontMenu; public void init() { setBackground(Color.white); add(new Label("Text String:")); inputField = new TextField(20); add(inputField); fontMenu = new Choice(); String[] fontArray = Toolkit.getDefaultToolkit().getFontList(); for (int i = 0; i < fontArray.length; i++) { fontMenu.addItem(fontArray[i]); } add(fontMenu); fontFace = fontArray[0]; } public void paint(Graphics g) { g.setFont(new Font(fontFace,Font.PLAIN,24)); g.drawString(displayText,150,60); } public boolean action(Event e, Object arg) { if (e.target instanceof TextField) { displayText = inputField.getText(); repaint(); return true; } if (e.target instanceof Choice) { fontFace = (String) arg; repaint(); return true; } return false; } }