Java Swing 实现猜数字小游戏

一、游戏界面如下

Java Swing 实现猜数字小游戏_第1张图片

Java Swing 实现猜数字小游戏_第2张图片

二、设计大概

1、两个界面:

(1)开始界面:

布局:BorderLayout

组件:JLabel x1,JButton x4

监听器:ActionListener

(2)游戏界面:
布局:FlowLayout

组件:JLabel x3,JTextField x1,JButton x1

2、功能实现

(1)关于、说明、设置难度

Java Swing 实现猜数字小游戏_第3张图片

  (2) 点击开始游戏,刷新JFrame

3)游戏界面显示剩余次数和使用次数。

Java Swing 实现猜数字小游戏_第4张图片

(4)猜大了、猜小了、猜对了提示

Java Swing 实现猜数字小游戏_第5张图片

 (5)游戏结束提示

Java Swing 实现猜数字小游戏_第6张图片

三:具体代码

1、窗口与功能


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class GuessNumTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new GameView();
	}

}
class GameView extends JFrame{
	//开始界面组件
	JLabel welcome;
	JButton about,instructions,gaming,hardSet;
	JPanel panelStart,panelGame;
	Font font = new Font("华文琥珀",Font.BOLD,56);
	//游戏界面组件
	JTextField input ;
	JLabel used,left;
	JButton submit;
	//难度、答案、剩余次数
	int hardN=6;//默认难度为6
	int answer;
	int chances=6;
	Listener listener  = new Listener();
	GameView() {
		//创建窗口
		this.setTitle("猜数字游戏");
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		gameStart();
		this.setLocation(300, 300);
		this.setSize(450,300);
		this.setVisible(true);
		listener.setView(this);
	}
	public void setHard(String s) {//设置难度
		try {
			int n = Integer.parseInt(s);
			this.hardN = n;
			chances = hardN;
		}
		catch(Exception e) {
			JOptionPane.showMessageDialog(this, "请输入正确的数字");
		}
		
	}
	public void gameStart() {
		//初始化开始界面
		welcome = new JLabel("     Welcome!  ");
		welcome.setPreferredSize(new Dimension(200,200));
		welcome.setFont(font);
		about = new JButton("关于");
		about.setBackground(Color.LIGHT_GRAY);
		instructions = new JButton("说明");
		instructions.setBackground(Color.LIGHT_GRAY);
		gaming = new JButton("开始游戏");
		gaming.setBackground(Color.pink);
		hardSet = new JButton("设置难度");
		hardSet.setBackground(Color.LIGHT_GRAY);
		BorderLayout bor = new BorderLayout();
		Box boxNorth = Box.createHorizontalBox();
		boxNorth.add(about);
		boxNorth.add(instructions);
		boxNorth.add(hardSet);
		this.add(boxNorth,BorderLayout.NORTH);
		this.add(welcome,"Center");
		this.add(gaming,"South");
		gaming.addActionListener(listener);
		about.addActionListener(listener);
		instructions.addActionListener(listener);
		hardSet.addActionListener(listener);
	}
	public void game() {
		//游戏开始时的准备操作
		this.getContentPane().removeAll();
		this.initGame();
		this.repaint();
		getANumber();
		System.out.println("答案:"+answer);
	}
	public void getANumber() {
		//得到一个0-100随机数字作为答案
		Random r = new Random();
		this.answer = r.nextInt(100);
	}
	public String judge() {
		//判断大小
		String text = input.getText();
		String regex = "\\d+";
		if(!text.matches(regex)) {
			JOptionPane.showMessageDialog(null, "Please input right number!");
			return "";
		}
		int guess = Integer.parseInt(text);
		if(guess>answer)return "big";
		if(guess

 2、监听器及消息处理



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

public class Listener implements ActionListener {
	GameView view;

	public void setView(GameView view) {
		this.view = view;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == view.hardSet) {//设置难度
			String s = JOptionPane.showInputDialog(view, "Please input a number to set the total chances:");
			if (s != null)
				view.setHard(s);
		}

		if (e.getSource() == view.about) {//关于
			String s = "author: Cyan\n2022.5.19";
			JOptionPane.showMessageDialog(view, s, "关于", JOptionPane.DEFAULT_OPTION);
		}
		if (e.getSource() == view.instructions) {//说明
			String s = "You will have six chances to guess a random number from 0 to 100.";
			JOptionPane.showMessageDialog(view, s, "说明", JOptionPane.DEFAULT_OPTION);
		}
		if (e.getSource() == view.gaming)//点击开始游戏
			view.game();
		if (e.getSource() == view.submit) {//提交答案
			String s = view.judge();    //判断答案
			if (s.equals(""))
				return;
			if (s.equals("big") || s.equals("small"))
				bigSmall(s);
			else
				right();

		}
	}

	private void bigSmall(String s) {
		// 猜大了、小了
		if (view.chances != 0) {
			String t = "Too " + s + " !\n Would like to continue?";  
			int n = JOptionPane.showConfirmDialog(view, t, "tips", JOptionPane.YES_NO_OPTION);
			if (n == JOptionPane.YES_OPTION) {
				view.changeChances();
			} else
				view.dispose();
		}
		if (view.chances == 0) {
			String mess1 = "What a pity! Chances used up.\n";
			String mess2 = "The right anwser is " + view.answer + "\n";
			String mess = mess1 + mess2 + "Try again?";
			int m = JOptionPane.showConfirmDialog(view, mess, "GameOver", JOptionPane.YES_NO_OPTION);
			newGameOrExit(m);
		}
	}

	private void right() {// 猜对了!
		String mess = "Congratulations! You get it with " + (view.hardN - (view.chances - 1)) + " times\n"
				+ "Play again?";
		int n = JOptionPane.showConfirmDialog(view, mess, "Win", JOptionPane.YES_NO_OPTION);
		newGameOrExit(n);
	}

	private void newGameOrExit(int m) {//游戏结束选择退出或继续
		if (m == JOptionPane.YES_OPTION) {
			view.dispose();
			view = new GameView();
		} else
			view.dispose();
	}

}

初学Swing ,还有很多不足,大家见谅

 

你可能感兴趣的:(java,小程序)