/*--------
*@ B.Lee
*用于生成一个窗口并处理各种事件
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class CreatWindow extends Frame implements ActionListener {
CloseDlg closed = new CloseDlg();// ///////////////////////////////////
static char thesysinwin[] = new char[4];// 用于暂存得到的数据
static char theuseinwin[] = new char[4];// 用于存随机数
// (貌似这两个初始化命名搞反了,但懒得改过来了)
static public int Greenc = 50;
static public int Redc = 137;
static public int Bluec = 200;
public int numbers = 0;// 计数
Compares comp = new Compares();// 比较类
ColorSet coset = new ColorSet();
public static CreatWindow frm = new CreatWindow(); // 实例化一个窗口
static Panel enbase = new Panel(new GridLayout(4, 3));// 创建一个四行三列的面板
static TextArea txt = new TextArea("", 10, 14,
TextArea.SCROLLBARS_VERTICAL_ONLY);// 创建一个文本框
static Label outbase = new Label(" ", Label.RIGHT); // 创建一个供响应并输出用户按键的面板
static Button btn7 = new Button("7");
static Button btn8 = new Button("8");
static Button btn9 = new Button("9");
static Button btn4 = new Button("4");
static Button btn5 = new Button("5");
static Button btn6 = new Button("6");
static Button btn1 = new Button("1");
static Button btn2 = new Button("2");
static Button btn3 = new Button("3");
static Button btn10 = new Button("0");
static Button btn11 = new Button("删除");
static Button btn12 = new Button("确定");
// ///////////////////////////////////////////////////////////////////////////////////////////////
static Button btn13 = new Button("重来");
static Button btn14 = new Button("颜色");
static Button btn15 = new Button("帮助");
static Button btn16 = new Button("退出");
// ///////////////////////////////////////////////////////////////////////////////////////////////
public void window() {
frm.setBounds(350, 300, 380, 200);// 设置窗口大小
frm.setVisible(true);// 设置窗口可见
frm.setResizable(false);// 设置不可改变大小
frm.setLayout(null);// 决定人工排列
frm.setBackground(new Color(Redc, Greenc, Bluec));// 页面背景颜色
frm.setTitle("猜数小游戏"); // 给窗口命名
outbase.setBounds(20, 40, 120, 20); // 设置输出面板的位置和大小
outbase.setBackground(Color.LIGHT_GRAY);// 设置输出面板的背景颜色为浅灰色
enbase.setBackground(Color.LIGHT_GRAY);
enbase.setBounds(20, 80, 120, 90);// 设置输入面板的位置和大小
enbase.add(btn7);
enbase.add(btn8);
enbase.add(btn9);
enbase.add(btn4);
enbase.add(btn5);
enbase.add(btn6);
enbase.add(btn1);
enbase.add(btn2);
enbase.add(btn3);
enbase.add(btn10);
enbase.add(btn11);
enbase.add(btn12);
// /////////////////////////////////////////////////////////////////////////
btn13.setBounds(330, 40, 40, 20);
btn14.setBounds(330, 80, 40, 20);
btn15.setBounds(330, 120, 40, 20);
btn16.setBounds(330, 160, 40, 20);
frm.add(btn13);
frm.add(btn14);
frm.add(btn15); // 四个功能键
frm.add(btn16);
// /////////////////////////////////////////////////////////////////////////
btn1.addActionListener(frm);
btn2.addActionListener(frm);
btn3.addActionListener(frm);
btn4.addActionListener(frm);
btn5.addActionListener(frm);
btn6.addActionListener(frm);
btn7.addActionListener(frm);
btn8.addActionListener(frm);
btn9.addActionListener(frm);
btn10.addActionListener(frm);
btn11.addActionListener(frm);
btn12.addActionListener(frm);
// ////////////////////////////////////////////////////////////////////////////////
btn13.addActionListener(frm);
btn14.addActionListener(frm);
btn15.addActionListener(frm);
btn16.addActionListener(frm);
// ////////////////////////////////////////////////////////////////////////////////
NewNumber newnum = new NewNumber();
theuseinwin = newnum.newsysnum();// 每次窗口初始化时产生新的随机数
frm.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
closed.creatclodlg();
closed.setBackground(new Color(Redc, Greenc, Bluec));
}
});// 关闭窗口
txt.setBounds(150, 40, 170, 130);// 设置文本框的位置和大小
txt.setEditable(false);// 设置文本不可编辑
frm.add(enbase);
frm.add(outbase);
frm.add(txt);
}
public void actionPerformed(ActionEvent e) {
Button btn = (Button) e.getSource();
String ans;
if (btn == btn1) {
if(comp.times!=0 && comp.times!=-1){
numbers = JudgeRigth.judge(thesysinwin, numbers, '1');
outbase.setText(JudgeRigth.ToString(thesysinwin, numbers));
}
} else if (btn == btn2) {
if(comp.times!=0 && comp.times!=-1){
numbers = JudgeRigth.judge(thesysinwin, numbers, '2');
outbase.setText(JudgeRigth.ToString(thesysinwin, numbers));
}
} else if (btn == btn3) {
if(comp.times!=0 && comp.times!=-1){
numbers = JudgeRigth.judge(thesysinwin, numbers, '3');
outbase.setText(JudgeRigth.ToString(thesysinwin, numbers));
}
} else if (btn == btn4) {
if(comp.times!=0 && comp.times!=-1){
numbers = JudgeRigth.judge(thesysinwin, numbers, '4');
outbase.setText(JudgeRigth.ToString(thesysinwin, numbers));
}
} else if (btn == btn5) {
if(comp.times!=0 && comp.times!=-1){
numbers = JudgeRigth.judge(thesysinwin, numbers, '5');
outbase.setText(JudgeRigth.ToString(thesysinwin, numbers));
}
} else if (btn == btn6) {
if(comp.times!=0 && comp.times!=-1){
numbers = JudgeRigth.judge(thesysinwin, numbers, '6');
outbase.setText(JudgeRigth.ToString(thesysinwin, numbers));
}
} else if (btn == btn7) {
if(comp.times!=0 && comp.times!=-1){
numbers = JudgeRigth.judge(thesysinwin, numbers, '7');
outbase.setText(JudgeRigth.ToString(thesysinwin, numbers));
}
} else if (btn == btn8) {
if(comp.times!=0 && comp.times!=-1){
numbers = JudgeRigth.judge(thesysinwin, numbers, '8');
outbase.setText(JudgeRigth.ToString(thesysinwin, numbers));
}
} else if (btn == btn9) {
if(comp.times!=0 && comp.times!=-1){
numbers = JudgeRigth.judge(thesysinwin, numbers, '9');
outbase.setText(JudgeRigth.ToString(thesysinwin, numbers));
}
} else if (btn == btn10) {
if(comp.times!=0 && comp.times!=-1){
numbers = JudgeRigth.judge(thesysinwin, numbers, '0');
outbase.setText(JudgeRigth.ToString(thesysinwin, numbers));
}
} else if (btn == btn11) {
numbers = JudgeRigth.judge(thesysinwin, numbers, 'B'); // 删除
outbase.setText(JudgeRigth.ToString(thesysinwin, numbers));
} else if (btn == btn12) {
numbers = JudgeRigth.judge(thesysinwin, numbers, 'G'); // 确定
if (numbers == 4) {
ans = comp.compare(thesysinwin, theuseinwin);
if (comp.times >= 0) {
if (ans.equals("CONGRATULATION")) {
txt.append(" " + ans + "S!!!\n"
+ JudgeRigth.ToString(theuseinwin, 4)
+ " IS THE ANSWER!\n");
numbers = 0;
comp.times = -1;
} else {
txt.append(JudgeRigth.ToString(thesysinwin, 4) + "---"
+ ans + "\t " + comp.times + "\n");
numbers = 0;
}
if (comp.times == 0) {
outbase.setText("The answer is"+ JudgeRigth.ToString(theuseinwin, 4));
txt.append(" You hava no chance.");
}
}
}
} else if (btn == btn13) { //重来
NewNumber newnum = new NewNumber();
if(comp.times!=-1 && comp.times!=0){
outbase.setText(" The answer is"
+ JudgeRigth.ToString(theuseinwin, 4));
try{ //
Thread.sleep(1000); // 此处显示结果,暂停3秒
}catch(InterruptedException ef)
{
} //
}
theuseinwin = newnum.newsysnum(); // 生成新的随机数
comp.times = 10;// 新的比较类
numbers = 0;// 将数组清空
txt.setText("");
outbase.setTe