我在使用按钮监听器时遇到了问题,因为我无法告诉按钮监听器哪个按钮是正确的答案。有没有办法将参数放入buttonListener中,以便我可以给它任何变量作为正确答案的信息,或者我应该放弃我尝试编码的方式?
我尝试传递一个数组列表作为正确答案的信息,但它不起作用。代码尚未完成,但如果不解决这个问题我就无能为力。
package Projecto;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
public class Main extends JFrame {
private JPanel panel;
private JLabel area, pergunta;
private JButton button1, button2, button3, button4, next, exit;
public static void main(String[] args) {
POOTrivia jogo = new POOTrivia();
ArrayList<Question> allquestoes = new ArrayList<>();
jogo.fileGetter(allquestoes);
ArrayList<Question> questions = new ArrayList<>();
while (questions.size() != 5) {
int get = (int) (Math.random() * allquestoes.size());
if (!questions.contains(allquestoes.get(get))) {
questions.add(allquestoes.get(get));
}
}
System.out.println(questions);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Main frame = new Main(questions);
frame.setTitle("Projecto.POOTrivia");
frame.setSize((screenSize.width), (screenSize.height));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
System.out.println(questions.get(1).getType());
new Main(questions);
}
public Main(ArrayList<Question> q) {
super();
int i = 0;
while (i < 5) {
String type = q.get(i).getType();
if (type.equals("Ciências")) {
Ciencias p = (Ciencias) q.get(i);
if (i < 3) {
area = new JLabel(p.getType());
pergunta = new JLabel(p.getQuestion());
ArrayList<String> options = new ArrayList<>();
options.add(p.getAnswear());
while (options.size() < 4) {
int r = (int) (Math.random() * p.getOptions().size());
if (!options.contains(p.getOptions().get(r))) {
options.add(p.getOptions().get(r));
}
}
//opcoes ficam aleatoriamente sorteadas
Collections.shuffle(options);
button1 = new JButton(options.get(0));
button2 = new JButton(options.get(1));
button3 = new JButton(options.get(2));
button4 = new JButton(options.get(3));
exit = new JButton("Sair");
next = new JButton("Próxima pergunta");
next.setVisible(false);
button1.addActionListener(new ButtonListener());
button2.addActionListener(new ButtonListener());
button3.addActionListener(new ButtonListener());
button4.addActionListener(new ButtonListener());
exit.addActionListener(new ButtonListener());
next.addActionListener(new ButtonListener());
panel = new JPanel();
panel.setLayout(new GridLayout(4, 2));
panel.add(area);
panel.add(pergunta);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(next);
panel.add(exit);
this.add(panel);
} else {
area = new JLabel(p.getType());
pergunta = new JLabel(p.getQuestion());
ArrayList<String> options = new ArrayList<>();
options.add(p.getDifficultAnswear());
while (options.size() < 4) {
int r = (int) (Math.random() * p.getDifficultOptions().size());
if (!options.contains(p.getDifficultOptions().get(r))) {
options.add(p.getDifficultOptions().get(r));
}
}
//opcoes ficam aleatoriamente sorteadas
Collections.shuffle(options);
button1 = new JButton(options.get(0));
button2 = new JButton(options.get(1));
button3 = new JButton(options.get(2));
button4 = new JButton(options.get(3));
exit = new JButton("Sair");
next = new JButton("Próxima pergunta");
next.setVisible(false);
button1.addActionListener(new ButtonListener());
button2.addActionListener(new ButtonListener());
button3.addActionListener(new ButtonListener());
button4.addActionListener(new ButtonListener());
exit.addActionListener(new ButtonListener());
next.addActionListener(new ButtonListener());
panel = new JPanel();
panel.setLayout(new GridLayout(4, 2));
panel.add(area);
panel.add(pergunta);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(next);
panel.add(exit);
this.add(panel);
}
i = 6;
} else if (type.equals("Futebol")) {
Futebol p = (Futebol) q.get(i);
i = 6;
} else if (type.equals("Ski")) {
Ski p = (Ski) q.get(i);
i = 6;
} else if (type.equals("Natação")) {
Natacao p = (Natacao) q.get(i);
i = 6;
} else if (type.equals("Artes")) {
Artes p = (Artes) q.get(i);
i = 6;
}
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent e) {
if(e.getActionCommand().equals()) {
if (button1.getText().equals("Ronaldo")) {
button1.setBackground(Color.GREEN);
}else if (button2.getText().equals("Ronaldo")) {
button2.setBackground(Color.GREEN);
} else if (button3.getText().equals("Ronaldo")) {
button3.setBackground(Color.GREEN);
}else if (button4.getText().equals("Ronaldo")) {
button4.setBackground(Color.GREEN);
}
next.setVisible(true);
} else if (e.getSource() == next) {
next.setVisible(false);
} else if (e.getSource() == exit) {
if (JOptionPane.showConfirmDialog(null, "Tem a certeza que pretende sair?", "Sair", JOptionPane.YES_NO_OPTION) == 0) {
System.exit(0);
}
} else {
if (button1.getText().equals("Ronaldo")) {
button1.setBackground(Color.GREEN);
button2.setBackground(Color.RED);
button3.setBackground(Color.RED);
button4.setBackground(Color.RED);
}else if (button2.getText().equals("Ronaldo")) {
button2.setBackground(Color.GREEN);
button1.setBackground(Color.RED);
button3.setBackground(Color.RED);
button4.setBackground(Color.RED);
} else if (button3.getText().equals("Ronaldo")) {
button3.setBackground(Color.GREEN);
button2.setBackground(Color.RED);
button1.setBackground(Color.RED);
button4.setBackground(Color.RED);
}else if (button4.getText().equals("Ronaldo")) {
button4.setBackground(Color.GREEN);
button2.setBackground(Color.RED);
button3.setBackground(Color.RED);
button1.setBackground(Color.RED);
}
next.setVisible(true);
}
}
}
}
您的代码无法编译,因为缺少
Question
和 POOTrivia
等类。
我继续创建了一个完整的 GUI,它可能可以满足您的需求。
Oracle 有一个有用的教程,使用 Swing 创建 GUI。跳过使用 NetBeans IDE 学习 Swing 部分。
当我创建 Swing GUI 时,我使用模型-视图-控制器 (MVC) 模式。这种模式允许我分离我的关注点并一次专注于应用程序的一个部分。看起来您试图这样做,但您的模型并未与您的视图完全分离。
Swing 模型由一个或多个普通 Java getter/setter 类组成。
Swing 视图由一个
JFrame
以及您需要的任意多个
JPanels
和
JDialogs
组成。每个 Swing
Action
或
Listener
都是一个控制器。通常没有一个控制器可以“统治所有”。型号
Question
类包含一个问题、四个可能的答案以及指向正确答案的索引指针。
TrivialGameModel
类拥有
List
个
Question
实例。我只创建了一个问题作为示例。查看
JFrame
和一个问题
JPanel
。我使用
GridBagLayout
来解决问题
JPanel
创建一个类似“表单”的结构。控制器
ActionListener
来检查所选答案是否正确。
代码这是完整的可运行代码。我创建了额外的类内部类,这样我就可以将代码作为一个块发布
JButton