猜数字游戏(Java实训)

猜数字游戏(Java实训)

在这里插入图片描述

实训目的:

掌握 Java GUI 编程和随机函数的使用。

实训要求:

每次产生一个 0-99 之间的随机数,用户输入猜测的数字后,
点击“试试”,系统将给出“大了”或“小了”的提示信息,帮助用户直到
猜对结果为止。实现效果如上图。

运用GUI设计即可。
在这里插入图片描述

Test.java

package program7;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;

public class Test {

	Random R = new Random();
	int temp = R.nextInt(100);
	private JFrame frame;
	private JTextField textField;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Test window = new Test();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Test() {
		initialize();
	}

	private void initialize() {
		frame = new JFrame();
		frame.setTitle("\u731C\u6570\u5B57\u6E38\u620F");
		frame.setBounds(100, 100, 800, 600);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JTextArea textArea = new JTextArea();
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(textArea);

		JLabel lblNewLabel = new JLabel("猜猜这个数字");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 17));

		JLabel lblNewLabel_1 = new JLabel("\u6570\u5B57\u8303\u56F4 0-99");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 17));

		textField = new JTextField();
		textField.setColumns(10);

		JButton button = new JButton("\u8BD5\u8BD5");
		textArea.append("游戏开始!\n");
		button.addActionListener(e -> {
			String content = textField.getText();
			if ((content != null) && (!content.trim().equals(""))) {
				int j = Integer.valueOf(content);
				if (j <= 99) {
					if (j > temp) {
						textArea.append("数字 " + content + " 猜大了\n");
					}
					if (j < temp) {
						textArea.append("数字 " + content + " 猜小了\n");
					}
					if (j == temp) {
						textArea.append("恭喜你,猜对了,数字为" + temp + "\n");
					}
				} else {
					textArea.append("输入的数字超过范围了,重新输入吧!\n");
				}
				textField.setText("");
			} else
				textArea.append("输入不为空,请重新输入!\n");
		});

		JButton button_1 = new JButton("\u770B\u7B54\u6848");
		button_1.addActionListener(e -> {
			textArea.append("答案是" + temp + "\n");
		});

		JButton btnNewButton = new JButton("\u91CD\u65B0\u5F00\u59CB");
		btnNewButton.addActionListener(e -> {
			textField.setText("");
			textArea.setText("");
			temp = R.nextInt(100);
			textArea.append("游戏重新开始!\n");
		});

		JButton button_2 = new JButton("\u9000\u51FA\u7A0B\u5E8F");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		frame.getContentPane().setLayout(groupLayout);
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup().addContainerGap().addComponent(lblNewLabel)
						.addPreferredGap(ComponentPlacement.RELATED).addComponent(lblNewLabel_1)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE).addGap(24)
						.addComponent(button).addPreferredGap(ComponentPlacement.RELATED).addComponent(button_1)
						.addPreferredGap(ComponentPlacement.RELATED).addComponent(btnNewButton)
						.addPreferredGap(ComponentPlacement.RELATED).addComponent(button_2)
						.addContainerGap(24, Short.MAX_VALUE))
				.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 794, Short.MAX_VALUE));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 461, GroupLayout.PREFERRED_SIZE)
						.addGap(32)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel)
								.addComponent(lblNewLabel_1)
								.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(button_2).addComponent(btnNewButton).addComponent(button_1)
								.addComponent(button))
						.addContainerGap(45, Short.MAX_VALUE)));
	}
}

结果展示:
在这里插入图片描述在这里插入图片描述在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值