java GUI 猜数字游戏

Posted 薄灬荷

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java GUI 猜数字游戏相关的知识,希望对你有一定的参考价值。


界面

 1 package com.lovo.hilo;
 2 
 3 import java.awt.Container;
 4 import java.awt.GridLayout;
 5 import java.awt.Toolkit;
 6 
 7 import javax.swing.JFrame;
 8 
 9 public class HiLoFrame extends JFrame {
10 
11     private Container contentP;
12 
13     private UpPanel upP;
14 
15     private MidPanel mdP;
16 
17     private DownPanel dnP;
18     
19     private int randomNum;
20 
21     public HiLoFrame() {
22         this.randomNum = (int)(Math.random() * 50 + 50);
23         System.out.println(this.randomNum);
24         // 工具箱类--ToolKit
25         Toolkit tk = Toolkit.getDefaultToolkit();
26         // 设置窗体大小
27         this.setSize(320, 290);
28         // 设置窗体位置
29         int screenW = (int) tk.getScreenSize().getWidth();// 得到屏幕宽
30         int screenH = (int) tk.getScreenSize().getHeight();// 得到屏幕高
31         this.setLocation((screenW - 320) / 2, (screenH - 290) / 2);
32         // 设置窗体大小不可更改
33         this.setResizable(true);
34         // 设置窗体图标
35         this.setIconImage(tk.createImage("img/hp.JPG"));
36         // 设置窗体的标题
37         this.setTitle("HiLo游戏");
38         // 设置窗体关闭即为关闭程序
39         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
40         // 设置窗体内容面板上所有的东西
41         this.addContent();
42         // 设置窗体可见
43         this.setVisible(true);
44     }
45 
46     
47 
48     private void addContent() {
49         // TODO Auto-generated method stub
50         this.contentP = this.getContentPane();
51         this.contentP.setLayout(new GridLayout(3, 1));
52         
53         this.upP = new UpPanel();
54         this.mdP = new MidPanel();
55         this.dnP = new DownPanel(this);
56         
57         this.contentP.add(this.upP);
58         this.contentP.add(this.mdP);
59         this.contentP.add(this.dnP);
60     }
61 
62     public UpPanel getUpP() {
63         return upP;
64     }
65 
66     public MidPanel getMdP() {
67         return mdP;
68     }
69 
70     public DownPanel getDnP() {
71         return dnP;
72     }
73 
74     public int getRandomNum() {
75         return randomNum;
76     }
77     
78     public void setRandomNum(int randomNum) {
79         this.randomNum = randomNum;
80     }
81 
82 }


内部的功能
 1 package com.lovo.hilo;
 2 
 3 import java.awt.Color;
 4 
 5 import javax.swing.BorderFactory;
 6 import javax.swing.JPanel;
 7 import javax.swing.JTextField;
 8 
 9 //JPanel的默认布局管理是流布局
10 public class UpPanel extends JPanel{
11     
12     private JTextField inputTxt;
13     
14     public UpPanel(){
15         this.setBackground(Color.WHITE);
16         this.setBorder(BorderFactory.createTitledBorder("Your Guess"));
17         
18         this.setLayout(null);
19         this.inputTxt = new JTextField();
20         this.inputTxt.setBounds(90, 33, 140,25);
21         this.add(this.inputTxt);
22     }
23 
24     public JTextField getInputTxt() {
25         return inputTxt;
26     }
27 
28     public void setInputTxt(JTextField inputTxt) {
29         this.inputTxt = inputTxt;
30     }
31 }



 1 package com.lovo.hilo;
 2 
 3 import java.awt.Color;
 4 import java.awt.FlowLayout;
 5 import java.awt.Font;
 6 
 7 import javax.swing.BorderFactory;
 8 import javax.swing.JLabel;
 9 import javax.swing.JPanel;
10 
11 public class MidPanel extends JPanel {
12     
13     private JLabel msgLab;
14     
15     public MidPanel(){
16         this.setBackground(Color.WHITE);
17         this.setLayout(new FlowLayout());
18         this.setBorder(BorderFactory.createTitledBorder("Hint"));
19         
20         this.msgLab = new JLabel("Let‘s Play HiLo");
21         this.msgLab.setFont(new Font("微软雅黑",Font.PLAIN,15));
22         this.add(this.msgLab);
23     }
24 
25     public JLabel getMsgLab() {
26         return msgLab;
27     }
28 
29     public void setMsgLab(JLabel msgLab) {
30         this.msgLab = msgLab;
31     }
32     
33 }
 1 package com.lovo.hilo;
 2 
 3 import java.awt.BorderLayout;
 4 import java.awt.Color;
 5 
 6 import javax.swing.JPanel;
 7 
 8 public class DownPanel extends JPanel {
 9     
10     private ButtonPanel btnP;
11     
12     public DownPanel(HiLoFrame frame){
13         this.setBackground(Color.WHITE);
14         this.setLayout(new BorderLayout());
15         
16         this.btnP = new ButtonPanel(frame);
17         this.add(this.btnP,BorderLayout.SOUTH);
18     }
19 
20     public ButtonPanel getBtnP() {
21         return btnP;
22     }
23 
24     public void setBtnP(ButtonPanel btnP) {
25         this.btnP = btnP;
26     }
27     
28 }

 1 package com.lovo.hilo;
 2 
 3 import java.awt.Button;
 4 import java.awt.Color;
 5 import java.awt.FlowLayout;
 6 import java.awt.event.ActionEvent;
 7 import java.awt.event.ActionListener;
 8 
 9 import javax.swing.JButton;
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12 
13 public class ButtonPanel extends JPanel {
14     
15     private JButton enterBtn;
16     
17     private JButton replayBtn;
18     
19     private int i;
20     
21     private HiLoFrame frame;
22     
23     //匿名内部类中不能访问外部类的局部变量,只能访问全局变量或常量
24     //这里的形参是一个欺骗编译器的动作
25     public ButtonPanel( HiLoFrame frame){
26         this.frame=frame;
27         this.setBackground(Color.WHITE);
28         this.setLayout(new FlowLayout());
29         
30         this.enterBtn = new JButton("确定");
31         this.replayBtn = new JButton("重玩");
32         this.replayBtn.setEnabled(false);
33         this.enterBtn.setEnabled(true);
34         this.enterBtn.addActionListener(new ActionListener() {            
35             @Override
36             public void actionPerformed(ActionEvent e) {
37                 // TODO Auto-generated method stub
38                 //1、取得输入的整数
39                 String inputStr = ButtonPanel.this.frame.getUpP().getInputTxt().getText();
40                 JLabel msgLab =ButtonPanel.this. frame.getMdP().getMsgLab();
41                 //2、取得随机数
42                 int randomNum =ButtonPanel.this. frame.getRandomNum();
43                 String msg = "";
44                 if(inputStr.matches("[5-9][0-9]")){
45                     int inputNum = Integer.parseInt(inputStr);
46                     //3、比较                    
47                     if(inputNum > randomNum){
48                         msg = "不好意思,你猜大了!";
49                         msgLab.setText(msg+"你还有"+(6-i)+"次机会");
50                     }else if(inputNum < randomNum){
51                         msg = "不好意思,你猜小了!";
52                         msgLab.setText(msg+"你还有"+(6-i)+"次机会");
53                     }else{
54                         msg = "恭喜你猜对了!";
55                         msgLab.setText(msg);
56                             ButtonPanel.this.replayBtn.setEnabled(true);
57                             ButtonPanel.this.enterBtn.setEnabled(false);
58                             i=0;
59                     }
60                     
61                 }else{
62                     msg = "请输入50到99的有效数字!";
63                     msgLab.setText(msg+"你还有"+(7-i)+"次机会");
64                 }
65                 i++;
66                 
67                 if(i==7){
68                     ButtonPanel.this.replayBtn.setEnabled(true);
69                     ButtonPanel.this.enterBtn.setEnabled(false);
70                     i=0;
71                 }
72             }
73         });
74         
75         this.replayBtn.addActionListener(new ActionListener(){
76 
77             @Override
78             public void actionPerformed(ActionEvent e) {
79                 // TODO Auto-generated method stub
80                 ButtonPanel.this.frame.setRandomNum((int)(Math.random() * 50 + 50)); 
81                 ButtonPanel.this.replayBtn.setEnabled(false);
82                 ButtonPanel.this.enterBtn.setEnabled(true);
83                 i=0;
84                 System.out.println(ButtonPanel.this.frame.getRandomNum());
85             }
86             
87         });
88         this.add(this.enterBtn);
89         this.add(this.replayBtn);
90         
91     }
92     
93 }
 1 package com.lovo.test;
 2 
 3 
 4 import com.lovo.cardlayout.CardFrame;
 5 import com.lovo.event.ColorFrame;
 6 import com.lovo.frame.BorderFrame;
 7 import com.lovo.frame.FlowFrame;
 8 import com.lovo.frame.GridFrame;
 9 import com.lovo.frame.MyFrame;
10 import com.lovo.hilo.HiLoFrame;
11 import com.lovo.qq.QQFrame;
12 
13 public class TestMain {
14 
15     public static void main(String[] args) {
16         // TODO Auto-generated method stub
17 
18         new HiLoFrame();
19 
20     }
21 
22 }

 

 

  

以上是关于java GUI 猜数字游戏的主要内容,如果未能解决你的问题,请参考以下文章

java猜数字游戏代码

python猜数字GUI版本V0.2

猜数字小游戏

C语言 猜数字游戏代码

Java猜数字游戏

Java猜数字游戏