public void windowClosing(WindowEvent e) {
System.exit(0);
}
のように、メソッドwindowClosing()を変更すれば良い。
その他のイベントをとらえて表示するには、
public void windowActivated(WindowEvent e) {
System.err.println(e);
}
のように変更する。これを
windowClosed(),
windowDeactivated(),
windowDeiconified(),
windowIconified(),
windowOpened()
についても行なえば良い。
次にラベルをいくつも並べるというのは、以下のプログラムのように main()メソッドを変更する。
public static void main (String args[]) {
TestWin f = new TestWin("Test Window");
GridLayout g = new GridLayout(2, 2);
f.setLayout(g);
f.add(new Label("Third"));
f.add(new Label("Forth"));
f.add(new Label("Fifth"));
f.add(new Label("Sixth"));
f.setLocation(100, 200);
f.pack();
f.show();
}
この例ではGridLayoutのインスタンスを縦2、横2の大きさを指定して生成し、
setLayout()している。それからadd() でラベルを追加して
いっている。
ボタンを単に追加してみる練習については省略。「終了」ボタンを追加する分は、 まず文字列「Quit」であれば終了するようにメソッド actionPerformed() を次のように変更する。
public void actionPerformed (ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("Quit")) {
System.exit(0);
} else {
System.err.println(s);
}
}
次にmain()メソッドで終了用のボタンを追加するように、例えば次のように
変更する。
public static void main (String args[]) {
TestWin f = new TestWin("Test Window");
GridLayout g = new GridLayout(3, 1);
f.setLayout(g);
f.add(new Label("This is Label."));
Button b = new Button("Click Me");
b.addActionListener(f);
f.add(b);
b = new Button("Quit");
b.addActionListener(f);
f.add(b);
f.setLocation(100, 200);
f.pack();
f.show();
}
次に TextField を組み込む練習&宿題については次のようになる。
public static void main (String args[]) {
TestWin f = new TestWin("Test Window");
GridLayout g = new GridLayout(2, 1);
f.setLayout(g);
f.btn1 = new Button("q");
f.btn1.addActionListener(f);
f.add(f.btn1);
f.txt1 = new TextField(20);
f.txt1.addActionListener(f);
f.add(f.txt1);
f.setLocation(100, 200);
f.pack();
f.show();
}
public void actionPerformed (ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("q")) {
System.exit(0);
}
if (e.getSource() == txt1) {
try {
calc.push(new Float(txt1.getText()));
} catch (NumberFormatException e) {
System.err.println(e);
}
}
}
avalonではユーザが自分の作成したサーブレットを簡単に実行できる 仕組みを用意している。それを使用する場合は、以下のページを参照して前もっ て(少なくとも前日に)準備すること。
また、サーブレット関係ドキュメントは[こちら]に 準備しているので、詳細はそちらを参照すること。なお、今回用いるサーブレッ トの環境は第2.0版のものであり、かなり古い規格ではあるが、一通りの機能を 使ってみることが可能である。
1| import java.io.*;
2|
3| import javax.servlet.*;
4| import javax.servlet.http.*;
5|
6| /**
7| * This is a simple example of an HTTP Servlet. It responds to the GET
8| * and HEAD methods of the HTTP protocol.
9| */
10| public class Hello extends HttpServlet
11| {
12| /**
13| * Handle the GET and HEAD methods by building a simple web page.
14| * HEAD is just like GET, except that the server returns only the
15| * headers (including content length) not the body we write.
16| */
17| public void doGet (HttpServletRequest request,
18| HttpServletResponse response)
19| throws ServletException, IOException
20| {
21| PrintWriter out;
22| String title = "Example Apache JServ Servlet";
23|
24| // set content type and other response header fields first
25| response.setContentType("text/html");
26|
27| // then write the data of the response
28| out = response.getWriter();
29|
30| out.println("<HTML><HEAD><TITLE>");
31| out.println(title);
32| out.println("</TITLE></HEAD><BODY bgcolor=\"#FFFFFF\">");
33| out.println("<H1>" + title + "</H1>");
34| out.println("<H2>Congratulations, ApacheJServ 1.1.2 is working!<br>");
35| out.println("</BODY></HTML>");
36| out.close();
37| }
38| }
doGet()メソッドは HTTP の GET メソッドでこのサーブレットが指定
された際に呼び出される。HTTP のリクエストは第1引数に、レスポンス生成用の
オブジェクトは第2引数に渡される。
サーブレットはコンパイルしたあとあるタイミングでサーブレットエンジンに読 み込まれた際にインスタンスが生成され、ブラウザからのリクエストに応じてメ ソッドが呼び出される。
日本語を表示する際は、25行目を以下のように変更すること(もし、古いブラウ ザでうまく表示できない場合は、「UTF-8」の部分を 「EUC-JP」もしくは「ISO-2022-JP」にすれば良い)。
response.setContentType("text/html; charset=UTF-8");