java實現(xiàn)猜數字小游戲(Swing版)
2008年的時候,在學習Java how to program第五版的時候,寫過一個猜數字小游戲,是用Applet寫的;
現(xiàn)在,我要用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("游戲規(guī)則:已隨機生成一個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("猜低了,請繼續(xù)!已試錯"+(++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);
}
}
更多有趣的經典小游戲實現(xiàn)專題,也分享給大家:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
解決java web應用線上系統(tǒng)偶發(fā)宕機的情況
這篇文章主要介紹了解決java web應用線上系統(tǒng)偶發(fā)宕機的情況,具有好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java畢業(yè)設計實戰(zhàn)之校園一卡通系統(tǒng)的實現(xiàn)
這是一個使用了java+Springboot+Maven+mybatis+Vue+mysql+wd開發(fā)的校園一卡通系統(tǒng),是一個畢業(yè)設計的實戰(zhàn)練習,具有校園一卡通系統(tǒng)該有的所有功能,感興趣的朋友快來看看吧2022-01-01
springmvc Controller方法沒有加@ResponseBody導致api訪問404問題
這篇文章主要介紹了springmvc Controller方法沒有加@ResponseBody導致api訪問404問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
BufferedReader中read()方法和readLine()方法的使用
這篇文章主要介紹了BufferedReader中read()方法和readLine()方法的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
MyBatis綁定錯誤提示BindingException:Invalid bound statement (not f
這篇文章主要介紹了MyBatis綁定錯誤提示BindingException:Invalid bound statement (not found)的解決辦法,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-01-01

