JAVA講座 12時間目
チョイスを使って背景色を変えて、チョイスの使い方を覚えよう。
アプレット
ソースファイル
import java.applet.Applet;
import java.awt.*;
public class Study12 extends java.applet.Applet {
public void init() {
Choice c = new Choice();
c.addItem("black");
c.addItem("blue");
c.addItem("cyan");
c.addItem("darkGray");
c.addItem("gray");
c.addItem("green");
c.addItem("magenta");
c.addItem("pink");
c.addItem("red");
c.addItem("white");
c.addItem("yellow");
add(c);
}
public boolean action(Event e,Object o){
if(e.target instanceof Choice){
String s = (String)o;
if("black".equals(s)){
setBackground(Color.black);
}else if("blue".equals(s)){
setBackground(Color.blue);
}else if("cyan".equals(s)){
setBackground(Color.cyan);
}else if("darkGray".equals(s)){
setBackground(Color.darkGray);
}else if("gray".equals(s)){
setBackground(Color.gray);
}else if("green".equals(s)){
setBackground(Color.green);
}else if("magenta".equals(s)){
setBackground(Color.magenta);
}else if("pink".equals(s)){
setBackground(Color.pink);
}else if("red".equals(s)){
setBackground(Color.red);
}else if("white".equals(s)){
setBackground(Color.white);
}else if("yellow".equals(s)){
setBackground(Color.yellow);
}
}
repaint();
return true;
}
}
ソースファイルの説明
import java.applet.Applet;
import java.awt.*;
public class Study12 extends java.applet.Applet {
public void init() {
Choice c = new Choice();
c.addItem("black");
c.addItem("blue");
c.addItem("cyan");
c.addItem("darkGray");
c.addItem("gray");
c.addItem("green");
c.addItem("magenta");
c.addItem("pink");
c.addItem("red");
c.addItem("white");
c.addItem("yellow");
add(c);
}
public boolean action(Event e,Object o){
if(e.target instanceof Choice){
String s = (String)o;
if("black".equals(s)){
setBackground(Color.black);
}else if("blue".equals(s)){
setBackground(Color.blue);
}else if("cyan".equals(s)){
setBackground(Color.cyan);
}else if("darkGray".equals(s)){
setBackground(Color.darkGray);
}else if("gray".equals(s)){
setBackground(Color.gray);
}else if("green".equals(s)){
setBackground(Color.green);
}else if("magenta".equals(s)){
setBackground(Color.magenta);
}else if("pink".equals(s)){
setBackground(Color.pink);
}else if("red".equals(s)){
setBackground(Color.red);
}else if("white".equals(s)){
setBackground(Color.white);
}else if("yellow".equals(s)){
setBackground(Color.yellow);
}
}
repaint();
return true;
}
}
コメント文が書いていないところは既に前の時間で説明してあります。
13時間目に行く。
JAVA講座のページに戻る。
shimizu@eces.numazu-ct.ac.jp