import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class janken extends Applet{
Button btn;
Checkbox c0;
Checkbox c1;
Checkbox c2;
Label lb0;
Label lb1;
Label lb2;
Label lb3;
Label lb4;
CheckboxGroup cbG;
public void init() {
c0 = new Checkbox("グー");
c1 = new Checkbox("チョキ");
c2 = new Checkbox("パー");
btn = new Button("ジャンケン");
lb0 = new Label("相手");
lb1 = new Label(""); //相手が何を出したかを出力させる
lb2 = new Label("自分");
lb3 = new Label(""); //じゃんけんの結果を出力させる
lb4 = new Label("結果");
cbG = new CheckboxGroup();
c0.setCheckboxGroup(cbG);
c1.setCheckboxGroup(cbG);
c2.setCheckboxGroup(cbG);
c0.setState(true);
c1.setState(true);
c2.setState(true);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(c0.getState() == true){ //c0(グー)をチェックした場合
int i = (int) (3.0 * Math.random());
switch (i) {
case 0: //相手がグーのケース
lb1.setText("グー");
lb3.setText("あいこ"); //結果にあいこを表示
break;
case 1: //相手がチョキのケース
lb1.setText("チョキ"); //結果に勝ちを表示
lb3.setText("勝ち");
break;
case 2: //相手がパーのケース
lb1.setText("パー");
lb3.setText("負け"); //結果に負けを表示
break;
}
}else if(c1.getState() == true){ //c1(チョキ)をチェックした場合
int i = (int) (3.0 * Math.random());
switch (i) {
case 0:
lb1.setText("グー");
lb3.setText("負け");
break;
case 1:
lb1.setText("チョキ");
lb3.setText("あいこ");
break;
case 2:
lb1.setText("パー");
lb3.setText("勝ち");
break;
}
}else if(c2.getState() == true){ //c2(パー)をチェックした場合
int i = (int) (3.0 * Math.random());
switch (i) {
case 0:
lb1.setText("グー");
lb3.setText("勝ち");
break;
case 1:
lb1.setText("チョキ");
lb3.setText("負け");
break;
case 2:
lb1.setText("パー");
lb3.setText("あいこ");
break;
}
}
}
});
setLayout(null);
add(c0);c0.setBounds(10, 40, 60, 30);
add(c1);c1.setBounds(80, 40, 60, 30);
add(c2);c2.setBounds(150, 40, 60, 30);
add(btn);btn.setBounds(10, 80, 100, 30);
add(lb0);lb0.setBounds(10, 120, 60, 30);
add(lb1);lb1.setBounds(80, 120,60,30);
add(lb2);lb2.setBounds(10, 10,40,20);
add(lb3);lb3.setBounds(80, 160,50,20);
add(lb4);lb4.setBounds(10, 160,50,20);
}
}
|