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);


    }
}

原文地址:http://www.cnblogs.com/cy-xt/p/16852870.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性