アプレットを表示しているブラウザに、任意のURLを表示させる方法(JDK 1.4.1)
・htmlのソース
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta http-equiv="Pragma" content="no-cache">
<title>communication</title>
</head>
<body>
<applet code="Jump.class" codebase="." width="350" height="60">
</body>
</html>
・アプレットのソース
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
/**
* アプレットを表示しているブラウザに、任意のURLを表示させる。
*/
public class Jump extends Applet {
/** テキストフィールド */
private TextField textField;
/** ボタン */
private Button button;
/** ボタン用アクションリスナ */
private ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent e) {
actionPerformed_button(e);
}
};
/**
* アプレットの初期化処理
*/
public void init() {
textField = new TextField(40);
add(textField);
button = new Button("Jump");
button.addActionListener(listener);
add(button);
}
/**
* ボタンが押されたときの処理
* @param e アクションイベント
*/
private void actionPerformed_button(ActionEvent e) {
try {
URL url = new URL(getDocumentBase(), textField.getText());
getAppletContext().showDocument(url);
// getAppletContext().showDocument(url, "_blank");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
・実行結果
ボタンを押すとテキストフィールドに入力したURLに飛びます。