/*-------------------------------------- ex14_5.java イベント処理:ボタンを使う・画面クリア ---------------------------------------*/ /* */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class ex14_5 extends Applet implements ActionListener{ Dimension size; Button btn, btn_clr; // ボタン:宣言 String msg; int x, nx, z, app_wid, app_hei; boolean flg; public void init() { // 初期化処理 size = getSize(); app_wid = size.width; app_hei = size.height; x = 0; nx = 1; msg = x + ", " + nx; flg = false; btn = new Button("Fibonacci 数列を表示する"); btn_clr = new Button("画面クリア"); add(btn); add(btn_clr); btn.addActionListener(this); // アクションイベントを自クラスで受け取る宣言 btn_clr.addActionListener(this); // アクションイベントを自クラスで受け取る宣言 } public void paint(Graphics g) { // 描画処理 if (flg == false) { g.setColor(Color.green); g.setFont(new Font("Dialog", Font.PLAIN, 18)); g.drawString(msg, 10, 50); } else { g.clearRect(0, 0, app_wid-1, app_hei-1); msg = ""; flg = false; } } public void actionPerformed(ActionEvent e) { // アクションイベント実行処理 if (e.getSource() == btn) { // ボタンが押されたら… z = x + nx; x = nx; nx = z; msg += ", " + z; repaint(); } else if (e.getSource() == btn_clr) { flg = true; repaint(); } } }