java gui 猜数字游戏_java GUI猜数字游戏程序

展开全部

package j;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

@SuppressWarnings("serial")

public class J extends JFrame

{

JButton bRule;

JTextField Dnumber;

JButton bSure;

JButton bSurrender;

JButton bEnd;

JTextArea Dreasult;

int[] guessArray;

int[] randomNumber;

String sstr;

String reasult;

int x, y;

int n;

public J() //构造函数

{ //函数的声明

makenames();

layoutComponents();

setTitle("数字游戏");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

makenumber();

register();

}

//计时开始:

long startTime = System.currentTimeMillis();

long endTime;

public void makenames()

{

Dnumber = new JTextField(5);

Dreasult = new JTextArea();

bRule=new JButton("游戏规则");

bSure = new JButton("确定");

bSurrender=new JButton("投降");

bEnd=new JButton("结束32313133353236313431303231363533e58685e5aeb931333330363230");

reasult = new String("");

n = 0;

}

public void layoutComponents()

{

Container contentPane = this.getContentPane();

contentPane.setLayout(new GridLayout(4, 1));

JPanel Panel = new JPanel();

Panel.add(bRule);

bRule.setForeground(Color.red);

bRule.setFont(new Font("宋体",Font.BOLD,25));

JPanel Pane2 = new JPanel();

Pane2 .add(Dnumber);

Pane2.add(bSure);

JPanel Pane4 = new JPanel();

Pane4.add(bSurrender);

Pane4.add(bEnd);

JScrollPane scrollPane = new JScrollPane(Dreasult);

contentPane.add(Panel);

contentPane.add(Pane2);

contentPane.add(Pane4);

contentPane.add(scrollPane);

}

class bCancel implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

Dnumber.setText("");

}

}

public class bRule implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

JOptionPane.showMessageDialog(null, "系统自动生成一个4位随机数(每一位都不重复),请用户在“确定”键旁的文本框" +

"内输入一个您猜的4个数," +"\n"+

"如果猜得四位数中有位置与数值都正确的,则增加1个A;如果数值正确而位置不正确,则增加1个B。" +"\n"+

"用户猜数游戏直到用户猜对,按”结束“键结束;或用户投降,并反馈结果之后结束。",

"游戏规则", JOptionPane.PLAIN_MESSAGE);

}

}

public class bSure implements ActionListener {

boolean flag;

public void actionPerformed(ActionEvent e)

{

int m=0;

x = 0;

y = 0;

String str = new String(Dnumber.getText());

try{

int putIn = Integer.parseInt(str);

if(putIn<999||putIn>9999)

{

Dnumber.setText("");

JOptionPane.showMessageDialog(null, "必须是四个数字,请用户重新输入4位不重复的整数!",

"警告", JOptionPane.WARNING_MESSAGE);

}

guessArray = new int[4]; //把输入的一个4位数字变成数组

for (int i = 3; i >= 0; i--)

{

guessArray[0] = putIn/1000;

guessArray[1] = putIn%1000/100;

guessArray[2] = putIn%100/10;

guessArray[3] = putIn%10;

}

for(int i=0; i<4; i++) //如果有相同的数字,提示并要求重新输入

{

for(int j=i-1;j>=0;j--)

{

if(guessArray[i]==guessArray[j])

{

m++;

}

}

}

if (m>=1)

{

flag=true;

Dnumber.setText("");

JOptionPane.showMessageDialog(null, "4个数字都不能有相同,请重新输入4位不重复的整数!",

"注意", JOptionPane.PLAIN_MESSAGE);

}

}

catch(Exception e1)

{

JOptionPane.showMessageDialog(null, "输入不合法,请用户重新输入4位不重复的整数!");

Dnumber.setText("");

}

for (int i = 0; i <= 3; i++) //比较输入的与系统产生的,返回结果: xAyB

{

for (int j = 0; j <= 3; j++)

{

if (i == j)

{

if (guessArray[i] == randomNumber[j])

{

x++;

}

}

else

{

if (guessArray[i] == randomNumber[j])

{

y++;

}

}

}

}

n++;

//供编程者测试时间器:

//JOptionPane.showMessageDialog(null, sstr, "提示", JOptionPane.PLAIN_MESSAGE);

String a;

if (x == 4)

{

a = "第"+ n +"次" + " " + str + " " +x + "A" +

y +"B" + "\n" + "You are right!" + "\n"+sstr;

endTime=System.currentTimeMillis(); //计时结束

JOptionPane.showMessageDialog(null,"您总共猜了"+n+"次,共花了"+(endTime-startTime)/1000+

"秒的时间答对正确答案!");

}

else

{

a = "第"+ n +"次数" + " " + str + " " +x + "A" +

y +"B" + "\n";

}

Dreasult.setText(reasult+a);

Dnumber.setText("");

}

}

public class bSurrender implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

String Str="您太没有耐心了!本题的正确答案为:"+sstr;

JOptionPane.showMessageDialog(null, Str,

"投降", JOptionPane.PLAIN_MESSAGE);

System.exit(0);

}

}

public class bEnd implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

}

public void register() //注册

{

bRule r=new bRule();

bRule.addActionListener(r);

bSure s = new bSure();

bSure.addActionListener(s);

bSurrender su = new bSurrender();

bSurrender.addActionListener(su);

bEnd end = new bEnd();

bEnd.addActionListener(end);

}

private void makenumber() //生成四位无重复的随机数

{

randomNumber = new int[4];

Set s = new HashSet();

Random random = new Random();

while (s.size() < 4)

{

Integer i = new Integer(random.nextInt(10));

String x = i.toString();

if (!s.contains(x))

{

s.add(x);

}

}

Iterator t = s.iterator();

int ii = 0;

while (t.hasNext() && ii <= 3)

{

String x = (String) (t.next());

randomNumber[ii] = Integer.parseInt(x);

ii++;

}

sstr = s.toString();

}

public static void main(String[] args)

{

J Number = new J();

Number.setSize(new Dimension(600,350));//改变窗口大小

Number.setVisible(true);

JOptionPane.showMessageDialog(null,"请用户在左上角的对话框中输入4位不重复的整数!!");

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值