JAVA講座 10時間目


ボタンを使って背景色を変えて、ボタンの使い方を覚えよう。


アプレット




ソースファイル

import java.applet.Applet;
import java.awt.*;

public class Study10 extends java.applet.Applet {

  Button b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb;
  public void init() {
    b1 = new Button("black");
    add(b1);
    b2 = new Button("blue");
    add(b2);
    b3 = new Button("cyan");
    add(b3);
    b4 = new Button("darkGray");
    add(b4);
    b5 = new Button("gray");
    add(b5);
    b6 = new Button("green");
    add(b6);
    b7 = new Button("magenta");
    add(b7);
    b8 = new Button("pink");
    add(b8);
    b9 = new Button("red");
    add(b9);
    ba = new Button("white");
    add(ba);
    bb = new Button("yellow");
    add(bb);
  }

  public boolean action(Event e,Object o){
    if(e.target instanceof Button){
      if("black".equals(o)){
        setBackground(Color.black);
      }else if("blue".equals(o)){
        setBackground(Color.blue);
      }else if("cyan".equals(o)){
        setBackground(Color.cyan);
      }else if("darkGray".equals(o)){
        setBackground(Color.darkGray);
      }else if("gray".equals(o)){
        setBackground(Color.gray);
      }else if("green".equals(o)){
        setBackground(Color.green);
      }else if("magenta".equals(o)){
        setBackground(Color.magenta);
      }else if("pink".equals(o)){
        setBackground(Color.pink);
      }else if("red".equals(o)){
        setBackground(Color.red);
      }else if("white".equals(o)){
        setBackground(Color.white);
      }else if("yellow".equals(o)){
        setBackground(Color.yellow);
      }
    }
    repaint();
    return true;
  }
}

ソースファイルの説明 import java.applet.Applet; import java.awt.*; public class Study10 extends java.applet.Applet { Button b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb;//ボタンクラスのオブジェクトの定義 public void init() { b1 = new Button("black"); add(b1); b2 = new Button("blue"); add(b2); b3 = new Button("cyan"); add(b3); b4 = new Button("darkGray"); add(b4); b5 = new Button("gray"); add(b5); b6 = new Button("green"); add(b6); b7 = new Button("magenta"); add(b7); b8 = new Button("pink"); add(b8); b9 = new Button("red"); add(b9); ba = new Button("white"); add(ba); bb = new Button("yellow"); add(bb); } public boolean action(Event e,Object o){//コンポーネント内で何かイベントが発生したときに実行するメソッド です。引数として、evt(何のイベントが起きたのかを示すオブジェ クト),args(コンポーネントのラベル名の入ったオブジェクト)を待 ちます。 書式はpublic boolean action(Event evt,Object what)です。 if(e.target instanceof Button){//行われたイベントがButtonに関係するときは以下を実行します。 if("black".equals(o)){//ラベル名が「black」ならば、以下の行を実行します。 setBackground(Color.black); }else if("blue".equals(o)){ setBackground(Color.blue); }else if("cyan".equals(o)){ setBackground(Color.cyan); }else if("darkGray".equals(o)){ setBackground(Color.darkGray); }else if("gray".equals(o)){ setBackground(Color.gray); }else if("green".equals(o)){ setBackground(Color.green); }else if("magenta".equals(o)){ setBackground(Color.magenta); }else if("pink".equals(o)){ setBackground(Color.pink); }else if("red".equals(o)){ setBackground(Color.red); }else if("white".equals(o)){ setBackground(Color.white); }else if("yellow".equals(o)){ setBackground(Color.yellow); } } repaint(); return true; } } コメント文が書いていないところは既に前の時間で説明してあります。
11時間目に行く。

JAVA講座のページに戻る。

shimizu@eces.numazu-ct.ac.jp