I'm writing a piece of software that quizzes the user on musical notes, though I'll spare the musical details. Since my program is quite large, I wrote a separate program as a proof of concept and to work out my problem.
The simple program should work like this: In the main method, a number is randomly selected (1 or 2). The number is set as the text in the jlabel in the GUI. The user then presses one of the two number jbuttons. The user must then press another jbutton to confirm their selection. The main method compares the question and answer variables. If the numbers are equal, the text in the jlabel changes to "correct". Otherwise it changes to "incorrect". The user must then press a fourth jbutton to get the next question. The method then repeats and the user should be able to do as many exercises as they want.
My Test class which contains my main method
package test;
import java.util.Random;
public class Test {
protected static int question;
protected static int answer;
protected static boolean next = false;
protected static boolean sure = false;
protected static boolean exit = false;
protected static Random random = new Random();
public static void main(String[] args) {
GUI.gUI();
while(!exit){//while
question = random.nextInt(2) + 1;
GUI.lblNewLabel.setText("" + question);
while(!next){//next
while(!sure) {//sure
answer = Other.other();
}//sure
sure = false;
if(question == answer) {
GUI.lblNewLabel.setText("correct");
}
else {
GUI.lblNewLabel.setText("incorrect");
}
}//next
next = false;
}//exit
}
}
My GUI
package test;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class GUI extends JFrame {
private static final long serialVersionUID = 1L;
protected static int answerGUI;
protected static JLabel lblNewLabel = new JLabel("New label");
private JPanel contentPane;
/**
* Launch the application.
*/
public static void gUI() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
GUI frame = new GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
BTNListener btnlistener = new BTNListener();
lblNewLabel.setBounds(39, 25, 46, 14);
contentPane.add(lblNewLabel);
JButton btn1 = new JButton("No. 1");
btn1.setBounds(39, 158, 89, 23);
contentPane.add(btn1);
btn1.addActionListener(btnlistener);
JButton btn2 = new JButton("No. 2");
btn2.setBounds(157, 157, 89, 23);
contentPane.add(btn2);
btn2.addActionListener(btnlistener);
JButton btnAreYouSure = new JButton("Are you sure?");
btnAreYouSure.setBounds(38, 197, 99, 23);
contentPane.add(btnAreYouSure);
btnAreYouSure.addActionListener(btnlistener);
JButton btnNext = new JButton("Next");
btnNext.setBounds(159, 197, 89, 23);
contentPane.add(btnNext);
btnNext.addActionListener(btnlistener);
JButton btnExit = new JButton("Exit");
btnExit.setBounds(269, 198, 89, 23);
contentPane.add(btnExit);
btnExit.addActionListener(btnlistener);
}
protected class BTNListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "No. 1") {
Other.answer = 1;
}
else if(e.getActionCommand() == "No. 2") {
Other.answer = 2;
}
if(e.getActionCommand() == "Are you sure?") {
Test.sure = true;
}
if(e.getActionCommand() == "Next") {
Test.next = true;
}
if(e.getActionCommand() == "Exit") {
Test.exit = true;
}
}
}
}
My Other class
package test;
public class Other {
protected static int answer;
protected static int other() {
return answer;
}
}
I decided that I needed another class so the main method has another method to call upon as part of the sequence, though I suppose this method could be written in the GUI.
One of the reasons I've included a confirmation button is because after the first pass, the answer variable in the Other class already has a value, so on the second pass this value is immediately passed to the answer variable in Test and the user isn't able to change the value.
As the program is written right now, the jlabel displays the question value and none of the button presses seem to do anything. I tried using do...while loops instead and it worked for one pass before infinitely looping and not allowing the user to answer.