java实现猜数字小游戏(Swing版)

所属分类: 软件编程 / java 阅读数: 49
收藏 0 赞 0 分享

2008年的时候,在学习Java how to program第五版的时候,写过一个猜数字小游戏,是用Applet写的;
现在,我要用Swing重写这个小游戏,同时,加入一些新功能,如:背景颜色(红色表示偏高,蓝色表示偏低)、弹框、字体控制、布局管理器的使用。

运行截屏:

代码如下:

//Guess a number between 1 and 1000
//Java how to program, 10/e, Exercise 12.14
//by pandenghuang@163.com
/* (Guess-the-Number Game) Write an application that plays “guess the number” as follows:
Your application chooses the number to be guessed by selecting an integer at random in the range
1–1000. The application then displays the following in a label:
I have a number between 1 and 1000. Can you guess my number?
Please enter your first guess.
A JTextField should be used to input the guess. As each guess is input, the background color
should change to either red or blue. Red indicates that the user is getting “warmer,” and blue,
“colder.” A JLabel should display either "Too High" or "Too Low" to help the user zero in. When
the user gets the correct answer, "Correct!" should be displayed, and the JTextField used for
input should be changed to be uneditable. A JButton should be provided to allow the user to play
the game again. When the JButton is clicked, a new random number should be generated and the
input JTextField changed to be editable.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import static java.awt.BorderLayout.*;
 
public class NumberGuessGame2016 extends JFrame {
int number,random,counter=0;
JLabel welcomeJLabel;
JLabel hintJLabel;
JTextField guessField;
JPanel panel;//显示不同背景色
 
 
public NumberGuessGame2016() {
  super("猜数字小游戏游戏");
  setLayout(new BorderLayout());
  
  panel=new JPanel();
  panel.setBackground(Color.WHITE);
 
  welcomeJLabel= new JLabel("游戏规则:已随机生成一个1到1000的整数,您能在10次以内猜出来吗?");
  welcomeJLabel.setFont(new Font("Simsun",1,10));
  add(welcomeJLabel,NORTH);
 
  guessField=new JTextField(20);
  guessField.setFont(new Font("Arial",1,10));
  panel.add(guessField);
  add(panel); //默认添加到中间
 
  hintJLabel= new JLabel("");
  add(hintJLabel,SOUTH);
  hintJLabel.setFont(new Font("Simsun",1,10));
   
  TextFieldHandler handler=new TextFieldHandler();
  guessField.addActionListener(handler);
 
  random=(int)(1+1000*Math.random());
  
}
 
private class TextFieldHandler implements ActionListener 
{
  // process textfield events
 
  @Override
  public void actionPerformed (ActionEvent event)
  {
  while(true){
   number=Integer.parseInt(guessField.getText());
   while(number!=random)
   {
     number=Integer.parseInt(guessField.getText());
     if(number>random)
       {
       hintJLabel.setText("猜高了,不要放弃哦↖(^ω^)↗。已试错"+(++counter)+"次");
       guessField.setText("");
       panel.setBackground(Color.RED);
       }
     else
       {
      hintJLabel.setText("猜低了,请继续!已试错"+(++counter)+"次");
      panel.setBackground(Color.BLUE);
       guessField.setText("");
       }
   }
   //猜中后的用户提示
   if (counter<10)
    JOptionPane.showMessageDialog(null, "恭喜你,猜中了,难道你知道答案?O(∩_∩)O~");
   else if (counter==10)
    JOptionPane.showMessageDialog(null, "辛苦了,终于猜中了!");
   else
    JOptionPane.showMessageDialog(null, "您终于猜中了╮(╯▽╰)╭,您其实可以做得更好的!");
 
   //开始下一轮猜数字游戏前的初始化工作
   guessField.setText("");
   random=(int)(1+1000*Math.random());
   counter=0;
   
  }
  }
}
 
public static void main(String[] args)
{ 
  NumberGuessGame2016 f = new NumberGuessGame2016(); // create ListFrame
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.setSize(400,300); 
  f.setVisible(true); 
}
}

更多有趣的经典小游戏实现专题,也分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多