package com.itheima;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class CAISHUZIYOUXI {
public static void main(String[] args) {
JFrame jf=new JFrame();
jf.setTitle("聊天室");
jf.setSize(400,300);
jf.setDefaultCloseOperation(3);
jf.setLocationRelativeTo(null);
jf.setAlwaysOnTop(true);
jf.setLayout(null); //取消窗体的默认布局
//生成1-100的随机数
Random r =new Random();
int number = r.nextInt(100)+1;
//提示信息
JLabel messagelabel =new JLabel("系统产生了一个1-100之间的数据,请猜一猜");
messagelabel.setBounds(70,50,350,20);
jf.add(messagelabel);
//输入要猜的数字
JTextField numberField = new JTextField();
numberField.setBounds(120,100,150,20);
jf.add(numberField);
//猜数字的按钮
JButton GuessButton = new JButton("我猜");
GuessButton.setBounds(150,150,100,20);
jf.add(GuessButton);
GuessButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//判断猜的内容不能为空
String stringNumber = numberField.getText().trim();
if (stringNumber.equals("")){
JOptionPane.showMessageDialog(jf,"猜的数字不能为空");
return;
}
//每次根据猜的数字给出响应的提示
int guessnumber = Integer.parseInt(stringNumber);
if (guessnumber>number){
JOptionPane.showMessageDialog(jf,"你猜的数字大了");
numberField.setText("");
} else if (guessnumber<number) {
JOptionPane.showMessageDialog(jf,"你猜的数字小了");
numberField.setText("");
}else{
JOptionPane.showMessageDialog(jf,"恭喜你猜中了");
}
}
});
//添加按钮到窗体中
jf.setVisible(true);
}
}
标签:事务,JAVA,游戏,GuessButton,numberField,import,new,jf,100
From: https://www.cnblogs.com/cy-xt/p/16852870.html