利用图形用户界面设计实现“猜数字游戏”

1个回答

  • import java.awt.FlowLayout;

    import java.awt.GridLayout;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import javax.swing.JButton;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JOptionPane;

    import javax.swing.JPanel;

    import javax.swing.JTextField;

    public class TestGuessNumber extends JFrame {

    private static final long serialVersionUID = -4922390040187620343L;

    private JLabel label;

    private JTextField[] text = new JTextField[3];

    private JButton button;

    private String[] nums = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

    private String[] number;

    private int times;

    public static void main(String[] args) {

    new TestGuessNumber();

    }

    public TestGuessNumber() {

    JOptionPane.showMessageDialog(null, "。。。", "游戏规则",

    JOptionPane.INFORMATION_MESSAGE); // 。。。处填写规则,换行用“n”

    init();

    }

    private void init() {

    number = getNumber();

    this.setTitle("猜数字");

    this.setBounds(500, 400, 200, 160);

    this.setResizable(false);

    this.getContentPane().setLayout(new GridLayout(3, 1));

    label = new JLabel("请输入你猜的数字", JLabel.CENTER);

    this.getContentPane().add(label);

    JPanel panel = new JPanel(new FlowLayout());

    for (int i = 0; i < text.length; i++) {

    text[i] = new JTextField(3);

    panel.add(text[i]);

    }

    this.getContentPane().add(panel);

    button = new JButton("确定");

    button.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {

    for (JTextField tmp : text) {

    if (! tmp.getText().matches("^[0-9]{1}$")) {

    JOptionPane.showMessageDialog(null, "每个输入框请输入一位数字!");

    return;

    }

    }

    if (times++ >= 8) {

    if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null,

    "对不起,请再接再厉!n是否继续?", "失败", JOptionPane.YES_NO_OPTION)) {

    dispose();

    new TestGuessNumber();

    return;

    } else {

    dispose();

    System.exit(0);

    }

    }

    if (showResult()) {

    if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null,

    "你用了" + times + "次,猜对了数字!n是否继续?",

    "成功", JOptionPane.YES_NO_OPTION)) {

    dispose();

    new TestGuessNumber();

    } else {

    dispose();

    System.exit(0);

    }

    }

    }

    });

    this.getContentPane().add(button);

    this.addWindowListener(new WindowAdapter() {

    public void windowClosing(WindowEvent e) {

    dispose();

    System.exit(0);

    }

    });

    this.setVisible(true);

    // System.out.println(number[0] + number[1] + number[2]);

    }

    private String[] getNumber() {

    String[] num = new String[3];

    num[0] = nums[(int) (Math.random() * 10)];

    do {

    num[1] = nums[(int) (Math.random() * 10)];

    } while (num[1] == num[0]);

    do {

    num[2] = nums[(int) (Math.random() * 10)];

    } while (num[2] == num[0] || num[2] == num[1]);

    return num;

    }

    private boolean showResult() {

    int A = 0, B = 0;

    String num1 = text[0].getText().trim();

    String num2 = text[1].getText().trim();

    String num3 = text[2].getText().trim();

    if (num1.equals(number[0])) {

    A++;

    } else if (num1.equals(number[1]) || num1.equals(number[2])) {

    B++;

    }

    if (num2.equals(number[1])) {

    A++;

    } else if (num2.equals(number[0]) || num2.equals(number[2])) {

    B++;

    }

    if (num3.equals(number[2])) {

    A++;

    } else if (num3.equals(number[0]) || num3.equals(number[1])) {

    B++;

    }

    label.setText("第" + times + "次:" + A + "A" + B + "B");

    return A == 3 ? true : false;

    }

    }