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

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

}

}

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值