⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guess.java

📁 一个猜数字的java小游戏
💻 JAVA
字号:
/**
   BY HAOHUI.BIAN
   计科0503
*/

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.text.*;

public class Guess
{
   public static void main(String[] args)
   {
      ButtonFrame frame = new ButtonFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

/**
   A frame with a button panel
*/
class ButtonFrame extends JFrame
{
	private ButtonPanel P;
   public ButtonFrame()
   {
   		setTitle("QuessNumber");
    	Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();//得到屏幕尺寸
		int screenHeight = screenSize.height;
		int screenWidth = screenSize.width;
		setSize(screenWidth/2,screenHeight/2);//设置显示框尺寸
		setLocation(screenWidth/4,screenHeight/4);//设置显示框位置

      	// 创建按钮
      	P = new ButtonPanel();
      	//重新设置布局
      	setLayout(new BorderLayout());
      
      	JPanel Bpanel = new JPanel();
      	//第一个按钮
	  	JButton confirmButton = new JButton("确认");
	  	Bpanel.add(confirmButton);
	  	confirmButton.addActionListener(new
	  		ActionListener()
	  	{
	  		public void actionPerformed(ActionEvent event)
	  		{
	  			P.LikeActionPerformed(1);
	  		}
	  	});
	  
	  //第二个按钮
      JButton restartButton = new JButton("重新开始");
      Bpanel.add(restartButton);
      restartButton.addActionListener(new
	  ActionListener()
	  {
	  	public void actionPerformed(ActionEvent event)
	  	{
	  		P.LikeActionPerformed(2);
	  	}
	  });
	  //第三个按钮
      JButton exitButton = new JButton("退出");
      Bpanel.add(exitButton);
      exitButton.addActionListener(new
	  ActionListener()
	  {
	  	public void actionPerformed(ActionEvent event)
	  	{
	  		P.LikeActionPerformed(3);
	  	}
	  });

      // add panel to frame
	  add(Bpanel,BorderLayout.SOUTH);
	  add(P);
   }
   
   
}
      
      
// ButtonPanel
class ButtonPanel extends JPanel
{
	public static int count;//计数,表示用户猜了多少次
	public static int num;//用于表示玩家输入的猜测数
	public static int Tnum;//电脑生成的随机数,供玩家猜
	public static int choice;//标记,用于判别区分num和Tnum的三种大小情况
	public static int lownum = 1;//
	public static int upnum = 1000;//分别用于输出提供给玩家的猜测参考(猜测区间)
	public static JTextField textField;//文本域
	
	//无参构造函数
   	public ButtonPanel()
   {
      setLayout(null);//取消默认的布局管理
      textField = new JTextField(5);
      add(textField);
      textField.setBounds(200,150,80,20);//左上角的位置,宽度,高度确定文本域位置
      Tnum = MakeTnum();
   	     	  
   }
	//调用画图函数
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.drawString("你已经猜了"+count+"次",0,20);
		g2.drawString("输入猜测的数",120,165);
		switch(choice)
		{
			case 1: {	g2.drawString("太小",300,165);
						g2.drawString("大于"+lownum,0,70);
						g2.drawString("小于"+upnum,50,70);
					}break;
			case 2: {   g2.drawString("太大",300,165);
			            g2.drawString("大于"+lownum,0,70);
						g2.drawString("小于"+upnum,50,70);
					}break;
			case 3: g2.drawString("猜对了!",300,165);break;
		}
	}
	//获得用户输入的数据,即读取文本内容
   public void getNum()
   {
   		try{
   			num = Integer.parseInt(textField.getText().trim());
   		}	
   		catch(NumberFormatException e){}
   }
      
   public int MakeTnum()
   {
      return (int)(Math.random()*1000)+1;
   }
   //监听器   
   public void LikeActionPerformed(int actionid )//ActionListener唯一方法
   {
      	
		if(actionid == 1)
		{
			count ++;//玩家点击确认键后计数器加一
			getNum();//读取用户输入数
			if(num < Tnum)
			{
				lownum = lownum>=num?lownum:num;
				setBackground(Color.BLUE);
				choice = 1;//当玩家的输入小于正确值,置choice为1
			}
			
			else if(num > Tnum)
			{
				upnum = upnum<=num?upnum:num;
				setBackground(Color.RED);
				choice = 2;//当玩家的输入小于正确值,置choice为2
			}
			else if(num == Tnum)
			{
				setBackground(Color.WHITE);
				textField.setEditable(false);
				choice = 3;//当玩家的输入小于正确值,置choice为3
			}
		}
		else if(actionid == 2) 
		{
			textField.setEditable(true);
			getNum();
			setBackground(Color.WHITE);
			count = 0;
			lownum = 1;
			upnum = 1000;
			choice = 4;
			Tnum = MakeTnum();//随机数
		}
			
		else  System.exit(0);
		repaint();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -