我在这里尝试做的是我正在尝试创建一个类似于游戏的测验,其中用户猜测图片中的人物。如果每次点击提交按钮并且用户正确猜出图片,就会想要它。它应该进入下一个问题。我尝试过使用数组,但我不知道在哪里放置语法。而我很困惑。另外,我尝试过使用多态。但每次我点击提交按钮。下一张照片显示但分数和错误将消失。 -_-
这是我的代码。
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();
}
}
答案 0 :(得分:0)
您要在actionPerformed
方法中指定名称,但最好将其移至init()
方法。此外,ArrayList
将为您提供比固定大小的数组更多的灵活性。作为额外的奖励,您可以使用Collections.shuffle()
选择随机用户进行下一次猜测。
在游戏结束后,当你创建一个新的Second
对象时,得分和错误就会消失。最好重用已有的实例并根据需要隐藏/显示组件。