0

我在这里尝试做的是我正在尝试创建一个类似测验的游戏,用户可以在其中猜测图片中的人。如果每当我单击提交按钮并且用户正确猜测图片时,我会想要它。它应该进入下一个问题。我试过使用数组,但我不知道把语法放在哪里。我很困惑。另外,我尝试过使用多态性。但每次我点击提交按钮。下一张图片显示,但分数和错误会消失。-_-

这是我的代码。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Second extends JFrame{

    private int number, score;
    private String answer;
    private String[] answers;
    private ImageIcon[] questions;

    private JLabel lblQuestion, lblMistakes, lblScore;
    private JButton btnSubmit;
    private SubmitButtonHandler sbhandler;
    private JTextField txtAnswer;

    public Second(){

        setTitle("Guess Who");

        btnSubmit = new JButton("Submit");
        sbhandler = new SubmitButtonHandler();
        btnSubmit.addActionListener(sbhandler);

        txtAnswer = new JTextField(20);
        txtAnswer.setFont(new Font("Tekton Pro Ext", Font.BOLD, 20));
        txtAnswer.setHorizontalAlignment(JTextField.CENTER);

        lblMistakes = new JLabel("", JLabel.CENTER);
        lblMistakes.setForeground(Color.blue);
        lblMistakes.setFont(new Font("Tekton Pro Ext", Font.BOLD, 30));
        lblMistakes.setOpaque(true);

        lblScore = new JLabel("", JLabel.CENTER);
        lblScore.setForeground(Color.white);
        lblScore.setBackground(Color.black);
        lblScore.setFont(new Font("Tekton Pro Ext", Font.BOLD, 30));
        lblScore.setOpaque(true);

        questions = new ImageIcon[10];

        questions[1] = new ImageIcon("C:\\Users\\ACER\\Desktop\\mario.jpeg");
        questions[2] = new ImageIcon("C:\\Users\\ACER\\Desktop\\ss.jpg");
        questions[3] = new ImageIcon("C:\\Users\\ACER\\Desktop\\dora.jpg");
        questions[4] = new ImageIcon("C:\\Users\\ACER\\Desktop\\vegeta.jpg");
        questions[5] = new ImageIcon("C:\\Users\\ACER\\Desktop\\kirby.jpg");

        lblQuestion = new JLabel(questions[1]);
        lblQuestion.setFont(new Font("Tekton Pro Ext", Font.BOLD, 18));

        Container pane = getContentPane();

        pane.setLayout(new GridLayout(5,1));

        pane.add(lblQuestion);
        pane.add(txtAnswer);
        pane.add(btnSubmit);
        pane.add(lblScore);
        pane.add(lblMistakes);

        setBounds(500,200,500,500);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

    }

private class SubmitButtonHandler implements ActionListener{

    public void actionPerformed(ActionEvent e){

        answers = new String[10];

        answers[1] = "Mario";
        answers[2] = "Spongebob Squarepants";
        answers[3] = "Dora the Explorer";
        answers[4] = "Vegeta";
        answers[5] = "Kirby";

        if(txtAnswer.getText().equalsIgnoreCase(answers[1])){
            JOptionPane.showMessageDialog(null, "You got the correct answer.");
            number = number;
            score = score + 1;
            lblScore.setText("Score: " + Integer.toString(score));
            txtAnswer.setText("");
            txtAnswer.requestFocusInWindow();

        }else{
            number = number + 1;
            lblMistakes.setText("Mistakes: " + Integer.toString(number));
            txtAnswer.setText("");

            if(number == 1){
                JOptionPane.showMessageDialog(null, "Try again.");
                lblMistakes.setBackground(Color.yellow);
                txtAnswer.requestFocusInWindow();
            }else if(number == 2){
                lblMistakes.setBackground(Color.orange);
                JOptionPane.showMessageDialog(null,"Careful. You only have one chance.");
                txtAnswer.requestFocusInWindow();
            }else if(number == 3){
                lblMistakes.setBackground(Color.red);
                JOptionPane.showMessageDialog(null,"Game Over.");
                dispose();
                Second s = new Second();
            }

        }
        }

    }


public static void main(String[]args){

    Second s = new Second();


}
}
4

1 回答 1

0

您正在actionPerformed方法中分配名称,但最好将它们移动到init()方法中。此外,anArrayList将为您提供比固定大小的数组更多的灵活性。作为额外的奖励,您可以使用Collections.shuffle()随机选择一个用户进行下一次猜测。

Second一旦游戏结束,您正在创建一个新对象,分数​​和错误就会消失。最好重用您已有的实例并根据需要隐藏/显示组件。

于 2012-09-24T16:12:09.370 回答