// // URLButton.java // -------------- // // This is a simple Java applet that demonstrates how to "jump" to // an Internet URL from a Java applet when a button is pressed. // // Note: The on-screen message system could be improved, but the // purpose here is to demonstrate how to jump from an applet page // in a browser to a new URL when the button is pressed. // import java.awt.*; import java.applet.*; import java.net.*; public class URLButton extends Applet { // if the urlButton is clicked, try to jump to the URL // if the url is malformed, write a little message on-screen void urlButton_Clicked(Event event) { try { URL theURL = new URL(urlField.getText()); getAppletContext().showDocument(theURL); } catch (MalformedURLException mue) { msgLabel.setText("bad URL - try again"); } } public void init() { super.init(); //{{INIT_CONTROLS setLayout(null); addNotify(); resize(449,129); setBackground(new Color(16777215)); urlButton = new java.awt.Button("Go to URL"); urlButton.reshape(36,72,84,24); urlButton.setFont(new Font("Dialog", Font.PLAIN, 12)); add(urlButton); urlField = new java.awt.TextField(); urlField.setText("http://"); urlField.reshape(96,36,312,24); urlField.setFont(new Font("Dialog", Font.PLAIN, 12)); add(urlField); label1 = new java.awt.Label("URL:"); label1.reshape(36,36,48,24); add(label1); msgLabel = new java.awt.Label(""); msgLabel.reshape(144,72,264,24); msgLabel.setFont(new Font("Dialog", Font.PLAIN, 12)); msgLabel.setForeground(new Color(8421504)); add(msgLabel); //}} } public boolean handleEvent(Event event) { if (event.target == urlButton && event.id == Event.ACTION_EVENT) { urlButton_Clicked(event); return true; } return super.handleEvent(event); } //{{DECLARE_CONTROLS java.awt.Button urlButton; java.awt.TextField urlField; java.awt.Label label1; java.awt.Label msgLabel; //}} }