package learning_swing;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class homework2_gaijin {
static int count=5;
static int target=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
target=new Random().nextInt(101-0)+0;
JFrame jf=new JFrame("猜数字游戏!!!very 好玩!");//wo diu~~jue dui mei you lol haowan!
jf.setLayout(new BorderLayout());
JLabel jl1=new JLabel("请猜随机生成的数(0~100)",JLabel.CENTER);
JLabel jl2=new JLabel("剩余次数:"+5,JLabel.CENTER);
JTextField jt1=new JTextField();
JButton jb1=new JButton("提交");
JButton jl3=new JButton("提示器:gkd,猜数字,看看你是不是大聪明!");
jl3.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
count=5;
target=new Random().nextInt(101-0)+0;
jl2.setText("剩余次数:"+count);
jt1.setText("");
}
});
jb1.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(count>0) count-=1;
if(count<1) {
jl3.setText("提示器:机会用完了,你可以点击我重新开始游戏!");
}else {
int num=Integer.parseInt(jt1.getText());
if(num>target) {
jl3.setText("提示器:猜大了! >_<!");
}else if(num<target) {
jl3.setText("提示器:猜小了! qwq!");
}else {
jl3.setText("提示器:哇,猜对了!太强了,^_^!");
}
}
jl2.setText("剩余次数:"+count);
}
});
jf.add(jl1,BorderLayout.NORTH);
jf.add(jl2,BorderLayout.WEST);
jf.add(jt1,BorderLayout.CENTER);
jf.add(jb1,BorderLayout.EAST);
jf.add(jl3,BorderLayout.SOUTH);
jf.setBounds(500,400,300,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}