package javaxxx;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
public class MyProject extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
protected int randnumber;
protected int count;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyProject frame = new MyProject();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MyProject() {
setTitle("猜数游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("生成一个随机数,作为要猜测的数");
label.setBounds(10, 10, 202, 15);
contentPane.add(label);
JLabel label_1 = new JLabel("随机数范围下限:");
label_1.setBounds(10, 35, 116, 15);
contentPane.add(label_1);
textField = new JTextField();
textField.setBounds(136, 35, 76, 21);
contentPane.add(textField);
textField.setColumns(10);
JLabel label_2 = new JLabel("随机数范围上限:");
label_2.setBounds(10, 60, 116, 15);
contentPane.add(label_2);
textField_1 = new JTextField();
textField_1.setBounds(136, 66, 76, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
JTextArea textArea = new JTextArea();
textArea.setBounds(182, 100, 196, 23);
contentPane.add(textArea);
textArea.setEditable(false);
JButton button = new JButton("生成随机数");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String min1=textField.getText();
String max1=textField_1.getText();
int min=Integer.parseInt(min1);
int max=Integer.parseInt(max1);
// TODO Auto-generated method stub
Random rand=new Random();
randnumber = rand.nextInt(max-min)+min;
System.out.println(randnumber);
textArea.append("随机数已经生成,请开始你的表演");
textArea.paintImmediately(textArea.getBounds());
}
});
button.setBounds(31, 100, 115, 23);
contentPane.add(button);
JLabel label_4 = new JLabel("请输入你猜测的数:");
label_4.setBounds(10, 192, 126, 15);
contentPane.add(label_4);
JLabel label_3 = new JLabel("猜数");
label_3.setBounds(10, 149, 54, 15);
contentPane.add(label_3);
textField_2 = new JTextField();
textField_2.setBounds(146, 189, 66, 21);
contentPane.add(textField_2);
textField_2.setColumns(10);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBounds(222, 188, 178, 24);
contentPane.add(textArea_1);
textArea_1.setEditable(false);
JButton button_1 = new JButton("试试看");
button_1.setBounds(53, 217, 93, 23);
contentPane.add(button_1);
button_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
textArea_1.setText("");
String testNumber1=textField_2.getText();
int testNumber=Integer.parseInt(testNumber1);
System.out.println(testNumber);
if(testNumber
textArea_1.append("你猜的数小了");
textArea_1.paintImmediately(textArea_1.getBounds());
count++;
}else if(testNumber>randnumber) {
textArea_1.append("你猜的数大了");
textArea_1.paintImmediately(textArea_1.getBounds());
count++;
}else {
count++;
textArea_1.append("恭喜你猜对了,你一共试了"+count+"次");
textArea_1.paintImmediately(textArea_1.getBounds());
}
}
});