猜数游戏,过大过小提示,相等时结束(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(num1100||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

 

你可能感兴趣的:(Java基础)