自己和室友来黑马1103班学了半个月,用懂的知识编了个小游戏。代码很简单,但自己编出来的那一刻很高兴。我今年10月才开始学java,来到黑马视频辅导班也就半个月,发现现在越来越喜欢敲代码了,也有很多志同道合的兄弟一起学,很爽。- import java.awt.Button;
- import java.awt.Dialog;
- import java.awt.FlowLayout;
- import java.awt.Frame;
- import java.awt.Label;
- import java.awt.TextArea;
- import java.awt.TextField;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.util.Random;
- public class GuessNumberFinal {
- public static void main(String[] args) {
- // TODO 自动生成的方法存根
-
- new MouseAndKeyEvent();
- }
- }
- class MouseAndKeyEvent {
- private Frame f;
- private Button but,butBegin,btnOK;
- private TextField tf;
- private TextArea ta;
- private int count = 0,gCount=1,number;
- private boolean flag;
- private Dialog dl;
- private Label labInfo;
- private Random r;
- private GameTest gt;
-
- MouseAndKeyEvent() {
- r = new Random();
- number = r.nextInt(10000);
- init();
- }
- public MouseAndKeyEvent(int number) {
- // TODO 自动生成的构造函数存根
- this.number = number;
- init();
- }
-
-
- public void init() {
- f = new Frame("GuessNumber");
- f.setBounds(300, 100, 600, 500);
- f.setLayout(new FlowLayout());
- but = new Button("确认");
- butBegin = new Button("重新开始");
- tf = new TextField("请输入密码", 23);
- ta = new TextArea(10, 40);
- dl = new Dialog(f,"提示信息",true);
- labInfo = new Label();
- btnOK = new Button("确定");
- gt = new GameTest(number,gCount);
-
- dl.add(labInfo);
- dl.add(btnOK);
- dl.setBounds(400, 200, 350, 150);
- dl.setLayout(new FlowLayout());
- f.add(tf);
- f.add(but);
- f.add(butBegin);
- f.add(ta);
-
- myEvent();
- f.setVisible(true);
- }
- private void myEvent() {
- // TODO 自动生成的方法存根
- f.addWindowListener(new WindowAdapter() { //面板关闭按钮
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- but.addMouseListener(new MouseAdapter() { //面板确认按钮
- public void mouseClicked(MouseEvent e) {
- String m = tf.getText();
- // GameTest gt = new GameTest();
- System.out.println(m);
- if (flag) {
-
- ta.append(gt.run(m)+"\r\n");
- gCount = gt.getgCount();
- //System.out.println("1111"+gCount);
- }
- if (!flag) {
- if ("1".equals(m)) {
- ta.setText("游戏开始\r\n");
- flag = true;
- } else {
- labInfo.setText("你输入的\""+m+"\"密码错误,请重新输入"+"\r\n"+"还有"+(10-++count)+"机会");
- dl.setVisible(true);
- }
- while (count == 10) {
- System.exit(0);
- }
- }
-
- }
- });
-
- butBegin.addMouseListener(new MouseAdapter(){ //重新开始按钮
- public void mouseClicked(MouseEvent e){
- number = r.nextInt(1000);
- gCount = 1;
- gt = new GameTest(number,gCount);
- ta.setText("");
- }
- });
- tf.addMouseListener(new MouseAdapter() { //文本框单击清空
- public void mouseClicked(MouseEvent e) {
- tf.setText("");
- }
- });
-
- tf.addKeyListener(new KeyAdapter() {
- public void keyReleased(KeyEvent e){
- int code = e.getKeyCode();
- if(code==KeyEvent.VK_ENTER){
- String m = tf.getText();
- // GameTest gt = new GameTest();
- System.out.println(m);
- if (flag) {
-
-
-
- ta.append(gt.run(m)+"\r\n");
- gCount = gt.getgCount();
- System.out.println(gCount);
- }
- if (!flag) {
- if ("502495729".equals(m)) {
- ta.setText("游戏开始\r\n");
- flag = true;
- } else {
- labInfo.setText("你输入的\""+m+"\"密码错误,请重新输入"+"\r\n"+"还有"+(10-++count)+"机会");
- dl.setVisible(true);
- }
- while (count == 10) {
- System.exit(0);
- }
- }
-
- tf.setText("");
- }
- }
- });
-
- btnOK.addMouseListener(new MouseAdapter() { //确定,隐藏dialog
- public void mouseClicked(MouseEvent e) {
- if (!("请输入密码" == null)) {
- dl.setVisible(false);
- }
- }
- });
-
- btnOK.addKeyListener(new KeyAdapter() { //让butOK按下空格键是也能够实现功能
- public void keyReleased(KeyEvent e){
- int code = e.getKeyCode();
- if(code==KeyEvent.VK_SPACE){
- dl.setVisible(false);
- }
- }
- });
-
- dl.addWindowListener(new WindowAdapter() { //对话框关闭按钮单击后让对话框隐藏
- public void windowClosing(WindowEvent e) {
- dl.setVisible(false);
- }
- });
- }
- }
- class GameTest {
- private int number,gCount;
- public GameTest(){}
-
- public GameTest(int number,int gCount){
- this.number = number;
- this.gCount = gCount;
- //System.out.println(gCount);
- }
-
- public int getNumber() {
- return number;
- }
- public void setNumber(int number) {
- this.number = number;
- }
- public int getgCount() {
- System.out.println("....."+gCount);
- return gCount;
-
- }
- public void setgCount(int gCount) {
- this.gCount = gCount;
- }
- public String run(String m) {
- // 随机产生一个1~10000的数字
- if ("aa502495729".equals(m)) {// 作弊码,当录入为特定字符,直接提示答案
- ++gCount;
- return number + "";
-
- } else if (!(m.matches("[0-9]+"))) {// 判断录入的是否为数字
- ++gCount;
- return "请输入数字";
- } else {
- int d = Integer.parseInt(m);// 把字符转成int型和随机数字进行比较
- if (d > number) {// 判断数字
- ++gCount;
- return (m + "数字大了");
- } else if (d < number) {
- ++gCount;
- return (m + "数字小了");
- } else {
-
- return ("答案是"+number+"^_^ 恭喜你猜对了!!!!!!\r\n您经过"+gCount+"步完成游戏"+"\r\n请点击\"重新开始\"按钮,重新开始游戏。");
- }
- }
- }
- }
- class Mima {
- private int x = 522046817;
- public Mima() {
- }
- public int run() {
- return x;
- }
- }
复制代码
|