1. 程式人生 > 程式設計 >java實現猜數字小遊戲(Swing版)

java實現猜數字小遊戲(Swing版)

2008年的時候,在學習Java how to program第五版的時候,寫過一個猜數字小遊戲,是用Applet寫的;
現在,我要用Swing重寫這個小遊戲,同時,加入一些新功能,如:背景顏色(紅色表示偏高,藍色表示偏低)、彈框、字型控制、佈局管理器的使用。

執行截圖:

java實現猜數字小遊戲(Swing版)

java實現猜數字小遊戲(Swing版)

程式碼如下:

//Guess a number between 1 and 1000
//Java how to program,10/e,Exercise 12.14
//by [email protected]
/* (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",10));
  panel.add(guessField);
  add(panel); //預設新增到中間
 
  hintJLabel= new JLabel("");
  add(hintJLabel,SOUTH);
  hintJLabel.setFont(new Font("Simsun",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經典小遊戲彙總

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。