So basically I’m creating a sort of multiple choice test using Java and Swing, and I have two classes: the Main class and the Question class.I create question objects to store the information for questions, and I store the question as a JLabel, the answers as a JButton, and the correctAnswer as a JButton too. I have overloaded methods to create the question in my Main class, and I have a Question ArrayList to store the question objects as well. I have a loop to cycle to the next question when the quesiton is answered, the problem is when I try to implement this using ActionListener, it just doesn’t work. Either the ActionListener doesn’t pick up on it or something is going wrong somewhere.
Main.java file
package foo03;
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main implements ActionListener {
public static int count = 0;
static JLabel score = new JLabel("Score: " + count);
static JPanel panel = new JPanel();
JButton button = new JButton("Click me To Start");
static Question q1 = new Question();
Question q2 = new Question();
public static boolean qCount = false;
public Question tempQuestion = new Question();
public static ArrayList<Question> qlist = new ArrayList<Question>();
public static void increaseCount() {
count++;
}
public void actionPerformed(ActionEvent e) {
qCount = true;
if (e.getSource().equals(tempQuestion.correctAns)) {
count++;
}
}
public static void nextQuestion(Question q) {
if (qCount = true) {
panel.removeAll();
panel.add(score);
q.addQuestion(panel);
}
}
public Main() {
final JFrame frame = new JFrame();
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 100, 100));
panel.setLayout(new GridLayout(6, 6, 10, 10));
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Trivia!!");
frame.pack();
frame.setVisible(true);
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
// initializing the actual contents of the question
panel.add(score);
q1.createQuestion("What year was the declaration of independence signed", q1.correctAns, "2225", "brian zhUwU",
"1920", "1776");
q2.createQuestion("Who is the best basketball player?", "LeBron James", q2.correctAns,
"Pavan Cooper, Professional SPM", "Ben simmons", "Michael Jordan");
qlist.add(q1);
qlist.add(q2);
int iterationCount = 0;
while (iterationCount <= (qlist.indexOf(q2) + 1)) {
if ((qCount == true) || (iterationCount == 0)) {
tempQuestion = qlist.get(iterationCount);
nextQuestion(qlist.get(iterationCount));
tempQuestion.addQuestion(panel);
qCount = false;
iterationCount++;
}
}
}
public static void main(String[] args) {
new Main();
}
}
My Question.Java file:
package firstGui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Question {
JLabel question = new JLabel();
JButton ans1 = new JButton();
JButton ans2 = new JButton();
JButton ans3 = new JButton();
JButton ans4 = new JButton();
JButton correctAns = new JButton();
String str;
public void createQuestion(String questionName, JButton answer1, String answer2, String answer3, String answer4,
String correct) {
correctAns.setText(correct);
question.setText(questionName);
ans1 = answer1;
ans2.setText(answer2);
ans3.setText(answer3);
ans4.setText(answer4);
}
public void createQuestion(String questionName, String answer1, JButton answer2, String answer3, String answer4,
String correct) {
correctAns.setText(correct);
question.setText(questionName);
ans1.setText(answer1);
ans2 = answer2;
ans3.setText(answer3);
ans4.setText(answer4);
}
public void createQuestion(String questionName, String answer1, String answer2, JButton answer3, String answer4,
String correct) {
correctAns.setText(correct);
question.setText(questionName);
ans1.setText(answer1);
ans2.setText(answer2);
ans3 = answer3;
ans4.setText(answer4);
}
public void createQuestion(String questionName, String answer1, String answer2, String answer3, JButton answer4,
String correct) {
correctAns.setText(correct);
question.setText(questionName);
ans1.setText(answer1);
ans2.setText(answer2);
ans3.setText(answer3);
ans4 = answer4;
}
public void addQuestion(JPanel panel) {
panel.add(question);
panel.add(ans1);
panel.add(ans2);
panel.add(ans3);
panel.add(ans4);
}
}
I’ve tried implementing the ActionListener using the Main class as one, creating ActionListener Objects, and even building it into the Question class so it’s used for each question, but nothing is working and I’m hardstuck. It’s just not working, I’m not really getting any errors it’s just not functioning.
sufferingthroughcode is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5