Back to Applets page
Click in the boxes below, select all and copy to grab the applet
HTML listing:
<HTML> <HEAD> <TITLE>Listing 8.3</TITLE> </HEAD> <BODY BGCOLOR=WHITE> <APPLET CODEBASE="classes" CODE="Applet3.class" WIDTH=500 HEIGHT=300> </APPLET> </BODY> </HTML>
Applet listing:
import java.awt.*; import java.applet.Applet; public class Applet3 extends Applet { TextField inputField; String displayText = "Enter a string to display"; int fontStyle = Font.PLAIN; public void init() { setBackground(Color.white); add(new Label("Text String:")); inputField = new TextField(25); add(inputField); add(new Label(" Font Style:")); add(new Checkbox("Bold",null,false)); add(new Checkbox("Italic",null,false)); } public void paint(Graphics g) { g.setFont(new Font("TimesRoman",fontStyle,24)); g.drawString(displayText,200,60); } public boolean action(Event e, Object arg) { Checkbox boxLabel; if (e.target instanceof TextField) { displayText = inputField.getText(); repaint(); return true; } if (e.target instanceof Checkbox) { boxLabel = (Checkbox) e.target; if (boxLabel.getLabel() == "Bold") { if (boxLabel.getState()) { fontStyle += Font.BOLD; } else { fontStyle -= Font.BOLD; } } if (boxLabel.getLabel() == "Italic") { if (boxLabel.getState()) { fontStyle += Font.ITALIC; } else { fontStyle -= Font.ITALIC; } } repaint(); return true; } return false; } }