java實(shí)現(xiàn)猜數(shù)字小游戲(Swing版)
2008年的時(shí)候,在學(xué)習(xí)Java how to program第五版的時(shí)候,寫(xiě)過(guò)一個(gè)猜數(shù)字小游戲,是用Applet寫(xiě)的;現(xiàn)在,我要用Swing重寫(xiě)這個(gè)小游戲,同時(shí),加入一些新功能,如:背景顏色(紅色表示偏高,藍(lán)色表示偏低)、彈框、字體控制、布局管理器的使用。
運(yùn)行截屏:
代碼如下:
//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 range1?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 colorshould 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. Whenthe user gets the correct answer, 'Correct!' should be displayed, and the JTextField used forinput should be changed to be uneditable. A JButton should be provided to allow the user to playthe game again. When the JButton is clicked, a new random number should be generated and theinput 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('猜數(shù)字小游戲游戲'); setLayout(new BorderLayout()); panel=new JPanel(); panel.setBackground(Color.WHITE); welcomeJLabel= new JLabel('游戲規(guī)則:已隨機(jī)生成一個(gè)1到1000的整數(shù),您能在10次以內(nèi)猜出來(lái)嗎?'); 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); //默認(rèn)添加到中間 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('猜高了,不要放棄哦?(^ω^)?。已試錯(cuò)'+(++counter)+'次'); guessField.setText(''); panel.setBackground(Color.RED); } else { hintJLabel.setText('猜低了,請(qǐng)繼續(xù)!已試錯(cuò)'+(++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, '您終于猜中了?(???)?,您其實(shí)可以做得更好的!'); //開(kāi)始下一輪猜數(shù)字游戲前的初始化工作 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); }}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,也分享給大家:
C++經(jīng)典小游戲匯總
python經(jīng)典小游戲匯總
python俄羅斯方塊游戲集合
JavaScript經(jīng)典游戲 玩不停
java經(jīng)典小游戲匯總
javascript經(jīng)典小游戲匯總
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python調(diào)用接口合并Excel表代碼實(shí)例2. .net如何優(yōu)雅的使用EFCore實(shí)例詳解3. Python快速將ppt制作成配音視頻課件的操作方法4. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖5. 一文透徹詳解.NET框架類型系統(tǒng)設(shè)計(jì)要點(diǎn)6. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫(huà)特效7. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)8. 通過(guò)Ajax方式綁定select選項(xiàng)數(shù)據(jù)的實(shí)例9. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車(chē)10. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析
