Java猜数游戏:编写一个猜数程序,该程序随机在1到1000的范围中选择一个供用户猜测的整数。界面上提供一个文本框来接收用户输入的猜测的数,如果用户猜得太大,则背景变为红色,如果猜得太小,背景变为蓝色

package text4;

import java.awt.event.*;

import javax.swing.*;            

import java.awt.*;  

import javax.swing.JTextField;

import java.util.Random;




public class Number implements ActionListener{
	int count = 0;
	int Number1;
	Random random;
	JPanel Jpanel;
	JLabel label1;
	JLabel label2;
	JLabel label3;
	JLabel label4;
	JButton Button1;
	JButton Button2;
	JButton Button3;
	JButton Button4;
	JTextField Text;
	
	public void set_Num()
	{
		random = new Random();
		Number1 = Math.abs(random.nextInt()%1001);
	}
	
	public  Number()
	{
		set_Num();
		System.out.println(Number1);
		
		JFrame Frame = new JFrame();
		Frame.setSize(300, 220);
		Jpanel = new JPanel();
		Jpanel.setSize(300, 220);
		Jpanel.setBackground(null);
		Jpanel.setLayout(null);
		
		label1 = new JLabel("你已经猜了" + count + "次");
		label1.setBounds(5, 0, 150, 30);
		label1.setVisible(false);
		
		label4 = new JLabel("答案是:" + String.valueOf(Number1));
		label4.setBounds(160, 0, 100, 30);
		label4.setVisible(false);
		
		label2 = new JLabel("输入猜测的数");
		label2.setBounds(30, 50, 100, 30);
		
		Text = new JTextField();
		Text.setBounds(120, 50, 60, 30);
		
		label3 = new JLabel();
		label3.setBounds(190, 50, 100, 30);
		label3.setVisible(false);
		
		Button1 = new JButton("确认");
		Button1.setMnemonic(KeyEvent.VK_I);
        Button1.addActionListener(this);
		Button1.setBounds(10, 110, 60, 30);
		
		Button2 = new JButton("重新开始");
		Button2.setMnemonic(KeyEvent.VK_I);
        Button2.addActionListener(this);
		Button2.setBounds(80, 110, 100, 30);
		
		Button3 = new JButton("退出");
		Button3.setMnemonic(KeyEvent.VK_I);
        Button3.addActionListener(this);
		Button3.setBounds(190, 110, 60, 30);
		
		Button4 = new JButton("查看答案!");
		Button4.setMnemonic(KeyEvent.VK_I);
        Button4.addActionListener(this);
		Button4.setBounds(80, 150, 100, 30);
		
		Jpanel.add(Button1);
		Jpanel.add(Text);
		Jpanel.add(Button2);
		Jpanel.add(Button3);
		Jpanel.add(Button4);
		Jpanel.add(label1);
		Jpanel.add(label2);
		Jpanel.add(label3);
		Jpanel.add(label4);
		Frame.add(Jpanel);
		Frame.setVisible(true);
	}
	public void actionPerformed(ActionEvent e) {  
		 if(e.getSource()==	Button1)
		{
			 label1.setVisible(true);
			 count ++;
			 label1.setText("你已经猜了" + count + "次");
			 String str = new String(Text.getText());
			 int Num2 = Integer.parseInt(str);
			 if(Num2 == Number1)
			 {
				 Text.setEditable(false);
				 Jpanel.setBackground(null);
				 label3.setVisible(true);
				 label3.setText("GOOD JOB!");
				 label4.setVisible(false);
				 Button1.setEnabled(false);
				 Button4.setEnabled(false);
			 }
			 else if(Num2 < Number1)
			 {
				 Jpanel.setBackground(Color.blue);
				 label3.setVisible(true);
				 label4.setVisible(false);
				 label3.setText("太小");
			 }
			 else
			 {
				 Jpanel.setBackground(Color.red);
				 label3.setVisible(true);
				 label4.setVisible(false);
				 label3.setText("太大");
			 }
		}
		 else if(e.getSource() == Button2)
		 {
			 label1.setVisible(false);
			 Text.setEditable(true);
			 label3.setVisible(false);
			 Button1.setEnabled(true);
			 Text.setText(null);
			 Jpanel.setBackground(null);
			 count = 0;
			 set_Num();
			 label4.setText("答案是:" + String.valueOf(Number1));
			 label4.setVisible(false);
			 Button4.setEnabled(true);
			 System.out.println(Number1);
		 }
		 else if(e.getSource() == Button3)
			 System.exit(0);
		 else
		 {
			 label4.setVisible(true);
		 }
    } 
	public static void main(String[] args) {
		@SuppressWarnings("unused")
		Number Num1;
		Num1 = new Number();
	}

}

你可能感兴趣的:(Java)