package test; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.ButtonGroup; import javax.swing.JOptionPane; import javax.swing.JRadioButton;
public class Test1 extends JFrame implements ActionListener {
private static final long serialVersionUID = 920921981358099067L;
public int sumMatch;
private JRadioButton jrJiandan,jrKunnan;
private JLabel lbPer, lbCom, lbLast,btnNandu;
private JButton btnStart, btnOne, btnTwo, btnThree;
private JTextField tfPcshow, tfLastshow;
private boolean win = true;
public Test1() {
super("21计科2班第六组拿火柴小游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 300);
setResizable(false);
initUI();
setLocationRelativeTo(null);
}
private void initUI() {
Container container = getContentPane();
ButtonGroup group = new ButtonGroup();
btnNandu = new JLabel ("请选择难度:");
btnNandu.setBounds(40, 10, 150, 30);
btnNandu.setForeground(new Color(255, 140, 0));
jrJiandan = new JRadioButton ("简单");
jrJiandan.setBounds(200, 10, 150, 30);
jrKunnan = new JRadioButton ("困难");
jrKunnan.setBounds(300, 10, 150, 30);
btnStart = new JButton("定义初始火柴数");
btnStart.setBounds(410, 100, 150, 30);
btnStart.setForeground(new Color(255, 140, 0));
lbPer = new JLabel("人拿火柴数");
lbPer.setBounds(40, 50, 120, 30);
lbPer.setForeground(new Color(255, 140, 0));
lbCom = new JLabel("计算机拿火柴数");
lbCom.setBounds(40, 100, 120, 30);
lbCom.setForeground(new Color(255, 140, 0));
lbLast = new JLabel("剩余火柴数");
lbLast.setBounds(40, 150, 120, 30);
lbLast.setForeground(new Color(255, 140, 0));
btnOne = new JButton("1");
btnOne.setBounds(210, 50, 80, 40);
btnOne.setForeground(new Color(255, 140, 0));
btnTwo = new JButton("2");
btnTwo.setBounds(310, 50, 80, 40);
btnTwo.setForeground(new Color(255, 140, 0));
btnThree = new JButton("3");
btnThree.setBounds(410,50 ,80 ,40 );
btnThree.setForeground(new Color (255 ,140 ,0));
tfPcshow=new JTextField();
tfPcshow.setBounds (210 ,100 ,150 ,30 );
tfPcshow.setForeground (new Color (255 ,140 ,0));
tfLastshow=new JTextField();
tfLastshow.setBounds (210 ,150 ,150 ,30 );
tfLastshow.setForeground (new Color (255 ,140 ,0));
btnStart.addActionListener(this);
btnOne.addActionListener(this);
btnTwo.addActionListener(this);
btnThree.addActionListener(this);
group.add(btnOne);
group.add(btnTwo);
group.add(btnThree);
container.setLayout(null);
container.add(btnNandu);
container.add(jrJiandan);
container.add(jrKunnan);
container.add(lbPer);
container.add(lbCom);
container.add(lbLast);
container.add(btnStart);
container.add(btnOne);
container.add(btnTwo);
container.add(btnThree);
container.add(tfPcshow);
container.add(tfLastshow);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnStart){
sumMatch=(int)(Math.random()*(50-20))+20;
tfPcshow.setText("初始火柴数为"+sumMatch+"根");
tfLastshow.setText("");
}
else if(e.getSource() instanceof JButton){
int k=Integer.parseInt(((JButton)e.getSource()).getText());
sumMatch-=k;
if (sumMatch == 0) {
JOptionPane.showMessageDialog(this, "恭喜你赢了!", "", JOptionPane.INFORMATION_MESSAGE);
} else if (sumMatch < 0) {
JOptionPane.showMessageDialog(this, "请点击定义初始火柴数按钮", "错误", JOptionPane.INFORMATION_MESSAGE);
}
if(jrJiandan.isSelected()){
tfPcshow.setText("计算机拿了"+computer1()+"根");
}else{
tfPcshow.setText("计算机拿了"+computer2()+"根");
}
tfLastshow.setText("火柴剩余"+sumMatch+"根");
if (!win) {
JOptionPane.showMessageDialog(this, "你输了,再试一次吧", "", JOptionPane.INFORMATION_MESSAGE);
win = true;
}
}
}
private int computer1() {
int ret = 0;
if (sumMatch <= 3 && sumMatch > 0) {
ret = sumMatch;
sumMatch = 0;
win = false;
} else if (sumMatch != 0) {
ret = (int) (Math.random() * (3 - 1)) + 1;
sumMatch -= ret;
}
return ret;
}
private int computer2() {
int ret = 0;
if(sumMatch%4!=0){
ret=sumMatch%4;
sumMatch-=ret;
}else{
ret=(int)(Math.random()*3)+1;
sumMatch-=ret;
}
if(sumMatch==0){
win=false;
}
return ret;
}
public static void main(String[] args) {
Test1 frame=new Test1();
frame.setVisible(true);
}
}