java Swing版猜数小游戏·源码

以前用C#做的,现在改为java版的了。源码拿出来给大家分享。

反正,自我感觉呢,代码写的很清楚,比较适合初学者学习。

好了废话不多说,上代码。

貌似,复制了即可运行。

/*
 * GuessNumForm.java
 *
 * Created on __DATE__, __TIME__
 */

package com.neusoft.rd.elf;

import javax.swing.JOptionPane;

/**
 *
 * @author  __USER__
 */
public class GuessNumForm extends javax.swing.JFrame {

	/** Creates new form GuessNumForm */
	public GuessNumForm() {
		initComponents();
		initMoreComponents();
	}

	/***********************************
	 * 这块区域是填写自己定义的窗口属性值;
	 * *********************************/
	private void initMoreComponents() {
		this.setTitle("猜数小游戏");
	}

	/***********************************
	 * 以上区域是填写自己定义的窗口属性值;
	 * *********************************/
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();
		jButton3 = new javax.swing.JButton();
		jButtonYes = new javax.swing.JButton();
		jButtonNo = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jTextArea1.setColumns(20);
		jTextArea1.setEditable(false);
		jTextArea1.setRows(5);
		jScrollPane1.setViewportView(jTextArea1);

		jButton3.setText("\u91cd\u6765");
		jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jButton3MouseClicked(evt);
			}
		});

		jButtonYes.setText("\u6709");
		jButtonYes.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jButtonYesMouseClicked(evt);
			}
		});

		jButtonNo.setText("\u6ca1");
		jButtonNo.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jButtonNoMouseClicked(evt);
			}
		});

		jLabel1.setForeground(new java.awt.Color(0, 51, 255));
		jLabel1.setText("\u73a9\u6cd5\u8bf4\u660e");
		jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseEntered(java.awt.event.MouseEvent evt) {
				jLabel1MouseEntered(evt);
			}

			public void mouseExited(java.awt.event.MouseEvent evt) {
				jLabel1MouseExited(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(64, 64, 64)
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										190,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addComponent(
																		jLabel1,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		51,
																		Short.MAX_VALUE)
																.addGap(18, 18,
																		18))
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						jButtonNo,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						59,
																						Short.MAX_VALUE)
																				.addComponent(
																						jButton3,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						jButtonYes,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						59,
																						Short.MAX_VALUE))
																.addContainerGap()))));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(36, 36, 36)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														jScrollPane1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														137,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jButtonYes)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																.addComponent(
																		jButtonNo)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																.addComponent(
																		jButton3)))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										14, Short.MAX_VALUE)
								.addComponent(jLabel1).addContainerGap()));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void jLabel1MouseExited(java.awt.event.MouseEvent evt) {
		this.jTextArea1.setText(this.CurrentText);
	}

	private void jLabel1MouseEntered(java.awt.event.MouseEvent evt) {
		this.CurrentText=this.jTextArea1.getText();
		this.jTextArea1
				.setText("请从1-127之间选择一个数字,然后看看列出的数字中有没有您选的这个,并点击对应的按钮,然后我就知道您选的是几了.Happy Time!");
	}

	private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
		this.ResetArgs();
		this.jTextArea1.setText(this.generateText(ClickCount));
	}

	private void jButtonNoMouseClicked(java.awt.event.MouseEvent evt) {
		if (this.ClickCount == 7) {
			if (!hasGivenResult) {
				this.guessedString = "1" + this.guessedString;
				GiveResult();
				hasGivenResult = true;
			}
		}
		if (this.ClickCount < 7) {
			this.guessedString = "1" + this.guessedString;
			this.ClickCount++;
			this.generatedString = this.generateText(this.ClickCount);
			this.jTextArea1.setText(this.generatedString);
		}
	}

	private void jButtonYesMouseClicked(java.awt.event.MouseEvent evt) {
		if (this.ClickCount == 7) {
			if (!hasGivenResult) {
				this.guessedString = "0" + this.guessedString;
				GiveResult();
				hasGivenResult = true;
			}
		}
		if (this.ClickCount < 7) {

			this.guessedString = "0" + this.guessedString;
			this.ClickCount++;
			this.generatedString = this.generateText(ClickCount);
			this.jTextArea1.setText(this.generatedString);
		}
	}

	/**************************
	 * ******自定义函数区域******
	 **************************/
	//清空所有的变量
	private void ResetArgs() {
		ClickCount = 1;
		generatedString = "";
		guessedString = "";
		guessedInt = -1;
		joking = false;
		hasGivenResult = false;

		subStr = "";//截取最后的串的前7位判断是不是玩家在开玩笑!
	}

	//第七次点击鼠标时计算用户所猜数字并显示
	public void GiveResult() {
		if (this.guessedString.length() >= 7) {
			this.subStr = this.guessedString.substring(0, 7);
			if ("0000000".equals(this.subStr) || "1111111".equals(this.subStr)) {
				this.joking = true;
				this.jTextArea1.setText("不跟不遵守游戏规则的人玩!");
				JOptionPane.showMessageDialog(this, "讨厌,不跟你玩了!");
			}
		}
		if (!joking) {
			this.guessedInt = this.BinaryStringtoInt(this.guessedString);
			this.jTextArea1.setText("您猜的数字是:" + this.guessedInt);
		}
	}

	//传入二进制的字符串,转成十进制整形。0110111--
	public int BinaryStringtoInt(String s) {
		int oneBit = -1;
		int allBit = 0;
		int len = s.length();
		for (int i = 0; i < len; i++) {
			oneBit = Integer.parseInt(s.charAt(i) + "");
			allBit += oneBit << (len - 1 - i);
		}
		return allBit;
	}

	//生成文本域中的数字字符串的方法
	public String generateText(int times) {
		String returnIntString = "";
		String binary = "";
		int value = -1;
		for (int i = 1; i < 128; i++) {
			binary = Integer.toBinaryString(i);
			int k = 8 - binary.length();
			for (int j = 1; j < k; j++) {//补充为7位二进制  0110111
				binary = "0" + binary;
			}
			if ('0' == binary.charAt(7 - times)) {//把每个符合条件的串,拼成十进制的输出串
				value = BinaryStringtoInt(binary);
				returnIntString += value + ",";
			}
		}
		return returnIntString.substring(0, returnIntString.length() - 1);//去掉最后一个逗号
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		GuessNumForm gNForm = new GuessNumForm();
		gNForm.setVisible(true);
		gNForm.jTextArea1.setLineWrap(true);
		gNForm.jTextArea1.setText(gNForm.generateText(1));
	}

	/***********************************
	 *********自定义变量区域***********
	 * *********************************/
	private int ClickCount = 1;
	private String generatedString = "";
	private String guessedString = "";
	private int guessedInt = -1;
	//程序健壮性参数
	private boolean joking = false;
	private boolean hasGivenResult = false;
	private String subStr = "";//截取最后的串的前7位判断是不是玩家在开玩笑!
	
	private String CurrentText="";

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton3;
	private javax.swing.JButton jButtonNo;
	private javax.swing.JButton jButtonYes;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextArea jTextArea1;
	// End of variables declaration//GEN-END:variables

}


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的猜数游戏Java Swing实现。在这个游戏中,玩家需要猜测一个1到100之间的随机数。每次猜测后,程序会告诉玩家猜的数字是太大了还是太小了,直到玩家猜中为止。 ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GuessNumberGame extends JFrame { private int targetNumber; private int numOfGuesses; private JLabel promptLabel; private JTextField guessTextField; private JButton guessButton; private JLabel resultLabel; public GuessNumberGame() { targetNumber = (int) (Math.random() * 100) + 1; numOfGuesses = 0; setTitle("Guess Number Game"); setSize(300, 150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); promptLabel = new JLabel("Guess a number between 1 and 100:"); guessTextField = new JTextField(10); guessButton = new JButton("Guess"); resultLabel = new JLabel(""); guessButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { int guess = Integer.parseInt(guessTextField.getText()); numOfGuesses++; if (guess == targetNumber) { resultLabel.setText("Congratulations! You guessed the number in " + numOfGuesses + " guesses."); guessButton.setEnabled(false); } else if (guess < targetNumber) { resultLabel.setText("Too low. Guess again."); } else { resultLabel.setText("Too high. Guess again."); } } catch (NumberFormatException ex) { resultLabel.setText("Invalid input. Please enter a number."); } guessTextField.setText(""); } }); setLayout(new GridLayout(4, 1)); add(promptLabel); add(guessTextField); add(guessButton); add(resultLabel); setVisible(true); } public static void main(String[] args) { GuessNumberGame game = new GuessNumberGame(); } } ``` 这是一个简单的猜数游戏,只有一个文本框用于输入猜测的数字,一个按钮用于提交猜测,以及一个标签用于显示结果。当玩家猜中数字时,按钮将被禁用,玩家需要重新启动程序来开始新游戏
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值