Back to Applets page
Click in the boxes below, select all and copy to grab the applet
HTML listing:
<HTML> <HEAD> <TITLE>Listing 8.4</TITLE> </HEAD> <BODY BGCOLOR=WHITE> <APPLET CODEBASE="classes" CODE="Applet4.class" WIDTH=500 HEIGHT=300> </APPLET> </BODY> </HTML>
Applet listing:
import java.awt.*; import java.applet.Applet; public class Applet4 extends Applet { TextField inputField; String displayText = "Enter a string to display"; int fontSize = 12; CheckboxGroup fontSizeField; public void init() { setBackground(Color.white); add(new Label("Text String:")); inputField = new TextField(20); add(inputField); add(new Label("Font Size:")); fontSizeField = new CheckboxGroup(); add(new Checkbox("12",fontSizeField,true)); add(new Checkbox("14",fontSizeField,false)); add(new Checkbox("18",fontSizeField,false)); add(new Checkbox("24",fontSizeField,false)); } public void paint(Graphics g) { g.setFont(new Font("TimesRoman",Font.PLAIN,fontSize)); g.drawString(displayText,200,60); } public boolean action(Event e, Object arg) { if (e.target instanceof TextField) { displayText = inputField.getText(); repaint(); return true; } if (e.target instanceof Checkbox) { fontSize = Integer.parseInt(fontSizeField.getCurrent().getLabel()); repaint(); return true; } return false; } }