Back to Applets page
Click in the boxes below, select all and copy to grab the applet
HTML listing:
<HTML> <HEAD> <TITLE>Listing 8.1</TITLE> </HEAD> <BODY BGCOLOR=WHITE> <APPLET CODEBASE="classes" CODE="Applet1.class" WIDTH=500 HEIGHT=300> </APPLET> </BODY> </HTML>
Applet listing:
import java.awt.*; import java.applet.Applet; import java.net.*; public class Applet1 extends Applet { TextField passwordField; boolean badPass = false; URL passwordURL; public void init() { setBackground(Color.white); try { passwordURL = new URL("http://www.chalcedony.com/java/"); } catch (MalformedURLException err) { passwordURL = this.getDocumentBase(); } add(new Label("Password")); passwordField = new TextField(10); passwordField.setEchoCharacter('*'); add(passwordField); } public void paint(Graphics g) { if (badPass) { g.drawString("Incorrect Password--try again",200,40); } } public boolean action(Event e, Object arg) { if (e.target instanceof TextField) { if (passwordField.getText().equals("peachpit")) { getAppletContext().showDocument(passwordURL); } else { badPass = true; repaint(); } return true; } return false; } }