猜数游戏代码-图形界面
目录
猜数游戏代码–图形界面
猜数游戏:1:由系统随机产生一个数字,由用户猜,并给出偏大偏小的信息,直到猜出正确答案,系统给出猜的次数
2:共分为三个等级,等级一为一位数,等级二为两位数,等级三为三位数
注意:要根据用户的不同的错误输入给出相应的处理方法,比如输入的不是数字,选择等级一而输入了两位数等等
效果图:
代码:
package game;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
public class GuessNumber extends JFrame {
JMenuBar mb; //构造菜单栏
JMenu mFile,mOption,mHelp; //菜单选项,(文件,选项,帮助)
JMenuItem mExit,mCopyright,mInformation; //菜单的下拉选项 (退出,版权信息,游戏信息)
Container c; //定义一个容器
JPanel Input; //创建面板
JLabel nanDu; //创建标签
JRadioButton chu; //创建三个单选按钮,选择难度
JRadioButton zhong;
JRadioButton gao;
ButtonGroup bg; //创建按钮组
JButton bOk;
JLabel lNum;
JTextField tf; //创建文本框
JButton cc;
JButton again;
int number=0;
int count=0;
int N=0;
int a=0,b=0;
private void init(){
//1、 得到内容网格,并设置布局为BorderLayout,所有组件都放到内容网格上
c=this.getContentPane(); //获得当前窗口的内容网格
c.setLayout(new BorderLayout()); //所有组件都放到内容网格上
//2 、初始化菜单,并把菜单放到内容网格的最上边
mb=new JMenuBar();
mFile=new JMenu("文件(F)");
mOption=new JMenu("选项(O)");
mHelp=new JMenu("帮助(H)");
mb.add(mFile);mb.add(mOption);mb.add(mHelp);
mExit=new JMenuItem("退出(E)");
mFile.add(mExit);
mInformation=new JMenuItem("游戏信息");
mCopyright=new JMenuItem("版权声明");
mHelp.add(mInformation);mHelp.addSeparator();mHelp.add(mCopyright);
c.add(mb,BorderLayout.NORTH); //将菜单添加到容器最上边
//版权声明的监听器
mCopyright.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "本游戏由暗伤无痕出品", "版权声明", JOptionPane.INFORMATION_MESSAGE);
}
});
//游戏信息监听器
mInformation.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "1. 系统会随机产生一个数,你可以输入你脑海中的一个数字,\n 系统会作出比较,并给出偏大或偏小的信息,然后继续猜测 ,"
+ " \n直至猜出系统产生的数\n"
+ "2. 可以选择难度,初级:一位数 中级:两位数 高级:三位数\n"
+ "3. 游戏结束,将给出所用次数\n", "游戏信息", JOptionPane.INFORMATION_MESSAGE);
}
});
//退出选项的监听器
mExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
GuessNumber.this.dispose();
}
});
//3、创建面板Input,放在内容网格的中间,用于放置主显示区的组件,布局为按照绝对位置存放组件
Input=new JPanel();Input.setLayout(null);
nanDu=new JLabel("游戏等级");
chu=new JRadioButton("初级",true);
zhong=new JRadioButton("中级");
gao=new JRadioButton("高级");
bg=new ButtonGroup();
bg.add(chu);bg.add(zhong);bg.add(gao); //将单选按钮添加到组中
Input.add(nanDu);Input.add(chu); Input.add(zhong); Input.add(gao); //将标签及单选按钮添加到Input面板上
nanDu.setBounds(10,15,60,20);
chu.setBounds(90,15,60,20); zhong.setBounds(150,15,60,20);gao.setBounds(210,15,60,20);
//确定按钮
bOk=new JButton("确定");
Input.add(bOk); //将确定按钮添加到Input面板上
bOk.setBounds(300,15,60,20);;
//"确定"按钮的监听器,接收等级
bOk.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(chu.isSelected()){
number=chu();
N=1;
}
else if(zhong.isSelected()){
number=zhong();
N=2;
}
else {
number=gao();
N=3;
}
tf.requestFocus();
}
});
//4.创建输入的文本框,标签,提交按钮
lNum=new JLabel("你能猜到我心中想的数字吗?");
Input.add(lNum);
lNum.setBounds(10,80,180,20);
tf=new JTextField();
Input.add(tf);
tf.setBounds(210,80,50,20);
//我猜猜
cc=new JButton("我要猜猜");
Input.add(cc);
cc.setBounds(290,68,86,20);
//“我要猜猜”按钮的监听器
cc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int a=0,b=0;
count++;
//判断是否为空
if(tf.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "请输入数字");return;
}
else{
//判断输入的是否是数字
char[] c=tf.getText().toCharArray(); //将字符串转换为字符数组
for (int i=0;i<c.length;i++) {
if (c[i]>='0'&&c[i]<='9') {
a++;
}
else {
b++;
}
}
if(a==c.length){
if(c.length!=N){
JOptionPane.showMessageDialog(null, "文盲,输错了,重输");
tf.setText("");
tf.requestFocus();
a=0;b=0;
}
else{
int n=Integer.parseInt(tf.getText().trim());
if(n>number){
JOptionPane.showMessageDialog(null, "你输入的数偏大了,再试试");
tf.setText("");
tf.requestFocus(); //文本框获取焦点
}
else if(n<number){
JOptionPane.showMessageDialog(null, "你输入的数偏小了,再试试");
tf.setText("");
tf.requestFocus();
}
if(n==number){
switch(count){
case 1:JOptionPane.showMessageDialog(null, "大神啊!你会读心术吗?一次就猜对了");
break;
case 2:
case 3:
case 4:JOptionPane.showMessageDialog(null, "不错不错,挺牛啊,这么快就猜到了\n"
+ "你一共猜了"+count+"次");break;
case 5:
case 6:
case 7:
case 8:
case 9:JOptionPane.showMessageDialog(null, "一般般啊!继续努力\n"+ "你一共猜了"+count+"次");break;
default:JOptionPane.showMessageDialog(null, "屌丝,猜这么多次才猜到\n"+"你一共猜了"+count+"次");break;
}
count=0;
tf.setText("");
tf.requestFocus();
}
}
}
else{
JOptionPane.showMessageDialog(null, "嗨,请输入数字");
tf.setText("");
tf.requestFocus();
a=0;b=0;
}
}
}
});
//重新开始
again=new JButton("重新开始");
Input.add(again);
again.setBounds(290,98,86,20);
again.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("");
count=0;
if(chu.isSelected())
number=chu();
else if(zhong.isSelected())
number=zhong();
else
number=gao();
tf.requestFocus();
}
});
c.add(Input,BorderLayout.CENTER); //将Input面板添加到容器中
this.setSize(400,300);
this.setVisible(true);
}
public GuessNumber(String title){
super(title);
init();
}
public int chu(){
Random random=new Random();
return random.nextInt(10);
}
public int zhong(){
Random random=new Random();
int n=random.nextInt(100);
while(n<10){
n=random.nextInt(100);
}
return n;
}
public int gao(){
Random random=new Random();
int n=random.nextInt(1000);
while(n<100){
n=random.nextInt(1000);
}
return n;
}
public static void main(String[] args){
GuessNumber gn=new GuessNumber("猜数游戏");
}
}