用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

给你一个1~1000的随机数请你猜,请输入你猜的数:

2018-06-19 作者: 云代码会员举报

[java]代码库

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Chp13_2 extends JFrame{
 
    private JButton chongwan;
    private JTextField field;
    private JLabel label1,label2;
    private Container container;
    private int shuzi = (int)(Math.random()*1000+1);
     
    Chp13_2 (){
         
        super("猜数游戏");
         
        container = getContentPane();
        container.setLayout(new FlowLayout());
         
        label1 = new JLabel("给你一个1~1000的随机数请你猜,请输入你猜的数:");
        container.add(label1);
         
        field = new JTextField(20);
        field.addKeyListener(new KeyListener(){
 
             
            public void keyPressed(KeyEvent arg0) {     }
             
            public void keyReleased(KeyEvent arg0) {
                 
                int shuru = Integer.parseInt(field.getText());
                if(shuru < shuzi ){
                    container.setBackground(Color.BLUE);
                    label2.setText("你猜小了!");
                }else if(shuru > shuzi){
                    container.setBackground(Color.RED);
                    label2.setText("你猜大了!");
                }else if(shuru == shuzi){
                    container.setBackground(Color.WHITE);
                    label2.setText("你猜对了!");
                    field.setEditable(false);
                }                              
            }
         
            public void keyTyped(KeyEvent arg0){        }
             
        });
         
         
         
         
        container.add(field);
         
        label2 = new JLabel("  ");
        container.add(label2);
         
        chongwan = new JButton("重玩");
        chongwan.addActionListener(new ActionListener(){
 
            public void actionPerformed(ActionEvent arg0) {
                 
                shuzi = (int)(Math.random()*1000+1);
                field.setEditable(true);
                 
            }
             
             
        });
         
         
        container.add(chongwan);
         
        setSize(500,400);
        setVisible(true);
         
         
    }
     
     
     
     
     
    public static void main(String[] args) {
         
        Chp13_2 application = new Chp13_2();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         
         
    }
     
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...