猜数游戏,过大过小提示,相等时结束(java)

package exe03;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;                        //*为全部的意思

/*
 * name: fourth2                          功能:猜数游戏,过大过小提示,相等时结束
 */

public class fourth2 extends JFrame  {
	
	//窗体需要两个标签,一个文本框,两个按钮
		private JLabel      txtLbl,warning1Lbl,warning2Lbl,warning3Lbl,warningLbl,numLbl,warLbl,war1Lbl;
		private JTextField  numField;
		private JButton     resertBtn,sureBtn;
		private int num1;   //输入的数字
		private int num;    //数的范围
		private String s;
		
	public fourth2 () {                     //构造函数
		//....................................创建窗体....................................
		                                //设置窗体标题
		super("猜数游戏");               //左上窗体名称
		setSize(500,350);               //设置窗体大小
	     
		
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);                    //设置关闭窗体 
		
		Image img=Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("Elevator.png"));
		this.setIconImage(img);                         //设置窗体图标
		WindowUtils.displayOnDesktopCenter(this);       //创建一个有关窗体的操作类,以便代码复用
		
		//....................................创建界面元素....................................
		txtLbl = new JLabel("请输入您猜测的数字:");
		
		
		
		numField = new JTextField(10);
		
		resertBtn = new JButton("重置");
		sureBtn = new JButton("确认");
		
		
		setLayout(null);	                            //设置窗口布局为空布局,必不可少的
		txtLbl.setBounds(180,70, 200, 30 );
		numField.setBounds(150, 100, 200, 30);
		resertBtn.setBounds(100, 200, 80, 20);
		sureBtn.setBounds(300, 200, 80, 20);
		
		
		add(txtLbl);
		add(numField);
		add(resertBtn);
		add(sureBtn);
		
		//设置背景色为黄色
		Container con=getContentPane();
		con.setBackground(Color.yellow); 
		//...................................开始生成随机数.....................................
		
		
		warningLbl= new JLabel(" ");                  //变量作用
	    add(warningLbl);
	    
	    war1Lbl = new JLabel(" ");                     //是否输入格式错误变量作用
		add(war1Lbl);
		
		
	    setVisible(true);                            //设置窗口可见
	    

		num = (int)(Math.random()*100+1);;             //生成随机数为Double型,所以强制转换, 生成1~100的数字	
	    s=String.valueOf(num);
	    
		numLbl= new JLabel();
		numLbl.setBounds(230,230,200,30);
		numLbl.setText(s);
		add(numLbl);
		setVisible(true);
		
	    
	 //确定按钮
	    sureBtn.addActionListener(new ActionListener() {
	    	
	    	public void actionPerformed(ActionEvent e) {
	    		// TODO 自动生成的方法存根	
	    		
	    		try {
	    		     String str = numField.getText();                         //获取JTextField里的内容
	    	         num1 = Integer.valueOf(str).intValue();                   //将string型的转换为int型
	    		
				}catch(Exception exc) {
					 num1 = 1101;
				}
	    	    
	    		if(num1>100||num1<0){
	    			//输入错误and提示
					war1Lbl.setVisible(false);
					warningLbl.setVisible(false);
					
	    			warLbl = new JLabel("输入有误!");
	    			
	    			add(warLbl);
	    			setVisible(true); 
	    			setLayout(null);	 
	    			warLbl.setBounds(220, 180, 80, 20);
	    			war1Lbl=warLbl;
	    			
	    			//设置背景色为黄色
	    			Container con=getContentPane();
	    			con.setBackground(Color.yellow); 
	    		}
	    		
	    		else if(num1>num){
	    			//输入错误and提示
					war1Lbl.setVisible(false);
	    			warningLbl.setVisible(false);
	    			
	    			warning1Lbl = new JLabel("TooLarge");
	    			add(warning1Lbl);
	    			setVisible(true); 
	    			setLayout(null);	 
	    			warning1Lbl.setBounds(200, 200, 100, 30);
	    			warningLbl=warning1Lbl;
	    			
	    			Container con=getContentPane();
	    			con.setBackground(Color.red);             //设置窗体背景颜色
	    			//numLbl.setVisible(false);                 
	    		}
	    		else if(num1==num) {
	    			//输入错误and提示
					war1Lbl.setVisible(false);
	    			warningLbl.setVisible(false);
	    			
	    			warning2Lbl = new JLabel("Right,Good!");
	    			add(warning2Lbl);
	    			setVisible(true); 
	    			setLayout(null);
	    			warning2Lbl.setBounds(200, 200, 100, 30);
	    			warningLbl=warning2Lbl;
	    			
	    			Container con=getContentPane();
	    			con.setBackground(Color.white);
	    			
	    			//numLbl.setVisible(false);                       
	    			sureBtn.setEnabled(false);               //确定按钮无用
	    			numField.setEnabled(false);              //输入框无用
	    		}
	    		else if(num1<num) {
	    			//输入错误and提示
					war1Lbl.setVisible(false);
	    		    warningLbl.setVisible(false);
	    		    
	    			warning3Lbl = new JLabel("TooSmall");
	    			add(warning3Lbl);
	    			setVisible(true);  
	    			setLayout(null);	 
	    			warning3Lbl.setBounds(200, 200, 100, 30);
	    			warningLbl=warning3Lbl;
	    			
	    			Container con=getContentPane();
	    			con.setBackground(Color.blue);             //设置窗体背景颜色
	    			//numLbl.setVisible(false);
	    		 }
	    		
	    	}
	    	  
		});
	  //重置按钮
	    resertBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根

				num = (int)(Math.random()*100+1);             //生成随机数为Double型,所以强制转换, 生成1~100的数字	
				
					
				//数字和提示语会发生变化
				numLbl.setVisible(false);
				warningLbl.setVisible(false);
				
				//输入错误
				war1Lbl.setVisible(false);
				
				//之前若是确定键和输入框无用,启用
				sureBtn.setEnabled(true);
				numField.setEnabled(true);
				
				warningLbl= new JLabel(" ");                  //变量作用
			    add(warningLbl);
			    setVisible(true); 
				
				//恢复黄色
				Container con=getContentPane();
    			con.setBackground(Color.yellow);
    			
				//之前的数字消失
    			s=String.valueOf(num);
    			
    			  
    			numLbl= new JLabel();
    			numLbl.setBounds(230,230,200,30);
    			numLbl.setText(s);
    			add(numLbl);
    			setVisible(true);
    			//文本框中内容消失
    			numField.setText("");
    			
    			
								
				warningLbl= new JLabel(" ");                  //变量作用
			    add(warningLbl);
			    
			    
			 //确定按钮  直接复制就好了
			    sureBtn.addActionListener(new ActionListener() {
			    	
			    	public void actionPerformed(ActionEvent e) {
			    		// TODO 自动生成的方法存根	
			    		
			    		try {
				    		String str = numField.getText();                         //获取JTextField里的内容
				    		
				    	    num1 = Integer.valueOf(str).intValue();                   //将string型的转换为int型
				    		
							}catch(Exception exc) {
								num1 = 1101;
							}
			    	    
			    		if(num1>100||num1<0){
			    			//输入错误and提示
							war1Lbl.setVisible(false);
							warningLbl.setVisible(false);
							
			    			warLbl = new JLabel("输入有误!");
			    			add(warLbl);
			    			setVisible(true); 
			    			setLayout(null);	 
			    			warLbl.setBounds(220, 180, 80, 20);
			    			war1Lbl=warLbl;
			    			
			    			//设置背景色为黄色
			    			Container con=getContentPane();
			    			con.setBackground(Color.yellow); 
			    		}
			    		
			    		else if(num1>num){
			    			//输入错误and提示
							war1Lbl.setVisible(false);
			    			warningLbl.setVisible(false);
			    			
			    			warning1Lbl = new JLabel("TooLarge");
			    			add(warning1Lbl);
			    			setVisible(true); 
			    			setLayout(null);	 
			    			warning1Lbl.setBounds(200, 200, 100, 30);
			    			warningLbl=warning1Lbl;
			    			
			    			Container con=getContentPane();
			    			con.setBackground(Color.red);             //设置窗体背景颜色
			    			//numLbl.setVisible(false);                 
			    		}
			    		else if(num1==num) {
			    			//输入错误and提示
							war1Lbl.setVisible(false);
			    			warningLbl.setVisible(false);
			    			
			    			warning2Lbl = new JLabel("Right,Good!");
			    			add(warning2Lbl);
			    			setVisible(true); 
			    			setLayout(null);
			    			warning2Lbl.setBounds(200, 200, 100, 30);
			    			warningLbl=warning2Lbl;
			    			
			    			Container con=getContentPane();
			    			con.setBackground(Color.white);
			    			
			    			//numLbl.setVisible(false);                       
			    			sureBtn.setEnabled(false);               //确定按钮无用
			    			numField.setEnabled(false);              //输入框无用
			    		}
			    		else if(num1<num) {
			    			//输入错误and提示
							war1Lbl.setVisible(false);
			    			warningLbl.setVisible(false);
			    			
			    			warning3Lbl = new JLabel("TooSmall");
			    			add(warning3Lbl);
			    			setVisible(true);  
			    			setLayout(null);	 
			    			warning3Lbl.setBounds(200, 200, 100, 30);
			    			warningLbl=warning3Lbl;
			    			
			    			Container con=getContentPane();
			    			con.setBackground(Color.blue);             //设置窗体背景颜色
			    			//numLbl.setVisible(false);
			    		 }
			    		
			    	}
			    	  
				});
			}
	    	
	    });
}
    public static void main(String[] args) {
        new fourth2();
    }
}






 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值