Answer the question
In order to leave comments, you need to log in
The function does not call the necessary padding for the interface based on the int variable, what does it need?
So, dear inhabitants of this resource, I am writing an elementary IQ test, but for the second time I stumble upon problems, the full description of this one looks something like this. There are two classes, an interface and a class with questions for the test, the interface is working properly. In the class with questions there are several functions, functions with content (question text, images), a function that calls one or another question depending on the variable int qx;
, and two functions that are responsible for the transition between questions, they change the value of that variable and then re-pass the question selection function to change the content of the interface. The functions work properly, the variable qx changes regularly, which is confirmed by others like it, the functions for changing the question call the function againSystem.out.println("nextQ"+qx);
public void chooseQuest();
but, the content of the interface does not want to change, although the function with the necessary content is called, tell me what's wrong.
Questions:
public class Question {
public String question;
public String img;
public boolean obrazek;
public boolean tlacitka;
public boolean textBox;
private int qx = 1;
public void nextQuestion(){
if(qx >= 1 & qx < 40){
qx = qx + 1;
}else if(qx == 40){
qx = 1;
}
System.out.println("nextQ"+qx);
chooseQuest();
}
public void prevQuestion(){
if(qx <= 40 & qx > 1){
qx = qx - 1;
}else if(qx == 1){
qx = 40;
}
System.out.println("prevQ"+qx);
chooseQuest();
}
/**
* Funkce ktera vyvolava zvolenou otazku
*/
public void chooseQuest(){
if(qx == 1){
Q1();
}else if(qx == 2){
Q2();
}
System.out.println("chQ"+qx);
}
/**
* Funkce ktera obsahuje prvky otazky 1(jedna)
*/
public void Q1(){
question = "Tady bude otazka1";
img = "img\\TASK1.png";
obrazek = true;
tlacitka = true;
textBox = false;
qx = 1;
System.out.println("Otazka 1");
}
public void Q2(){
question = "Tady bude otazka2";
img = "img\\TASK9.png";
obrazek = true;
tlacitka = true;
textBox = false;
qx = 2;
System.out.println("Otazka 2");
}
}
public UserInterface(){
setBounds(100,35,500,680);
setResizable(false);
setTitle("IQ test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(null);
quest = new Question();
JButton prevQ = new JButton("<<<");
prevQ.setBounds(120, 615, 60, 20);
prevQ.addActionListener(new pQuestButtonEvent());
panel.add(prevQ);
JButton nextQ = new JButton(">>>");
nextQ.setBounds(320, 615, 60, 20);
nextQ.addActionListener(new nQuestButtonEvent());
panel.add(nextQ);
JButton chButton = new JButton("Check Answer");
chButton.setBounds(190, 610, 120, 30);
chButton.addActionListener(new chButtonEvent());
panel.add(chButton);
quest.chooseQuest();
JTextField input = new JTextField("Answer here...");
input.setBounds(300, 550, 140, 20);
input.setVisible(quest.textBox);
panel.add(input);
JLabel questionT = new JLabel(quest.question);
questionT.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 102, 102), 1, true));
questionT.setBounds(10, 10, 470, 70);
panel.add(questionT);
ButtonGroup group = new ButtonGroup();
JRadioButton radio1 = new JRadioButton("Select 1");
JRadioButton radio2 = new JRadioButton("Select 2");
JRadioButton radio3 = new JRadioButton("Select 3");
JRadioButton radio4 = new JRadioButton("Select 4");
JRadioButton radio5 = new JRadioButton("Select 5");
JRadioButton radio6 = new JRadioButton("Select 6");
group.add(radio1);
group.add(radio2);
group.add(radio3);
group.add(radio4);
group.add(radio5);
group.add(radio6);
radioPanel = new JPanel(new GridLayout(0, 2));
radioPanel.add(radio1);
radioPanel.add(radio2);
radioPanel.add(radio3);
radioPanel.add(radio4);
radioPanel.add(radio5);
radioPanel.add(radio6);
radioPanel.setBounds(250, 80, 230, 450);
radioPanel.setVisible(quest.tlacitka);
panel.add(radioPanel);
img = new JLabel();
img.setBounds(10, 80, 230, 520);
img.setIcon(new javax.swing.ImageIcon(quest.img));
img.setVisible(quest.obrazek);
panel.add(img);
getContentPane().add(panel);
}
class nQuestButtonEvent implements ActionListener{
public void actionPerformed(ActionEvent e){
quest.nextQuestion();
}
}
class pQuestButtonEvent implements ActionListener{
public void actionPerformed(ActionEvent e){
quest.prevQuestion();
}
}
}
Answer the question
In order to leave comments, you need to log in
Hello. From the fact that you change the variables, the interface itself will not change automatically. You need to re-enter the values yourself with each update. For example, after the change quest.question
, you need to call questionT.setText(quest.question)
. I advise you to create a method that accepts a Question and puts its data into the interface elements.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question