🔒 Closed Pa help po java gui

Status
Not open for further replies.

Legend_00

Enthusiast
Ayawpo kaseng mapalitan nung question yung nasa loob ng jtextarea wherein dapat po napapalitan kase po nag iterate naman po yung iterator ko na nag a-access ng arraylist ko parang naka intact lang po sya sa unang question na nilagay nung user which is 0 parang siman po tlga sya na iterate ket tama naman po yung logic ko

eto po yung source code ko:


package ACEQUIZ;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class ACEQUIZ{

JFrame ACEQUIZ1,SecondFrame,ThirdFrame,FourthFrame;
JLabel label1, label2, label3, quesLabel, ansLabel, spacer1,spacer2,
playQuesLabel, playAnsLabel, spacerQuestion;
JPanel panelsContainer, panel1, panel2, panel3, startButtonPanel,
secondMainPanel,QandApanel, ThirdMainPanel, playQandApanel;
JButton startButton,addButton, playButton, nextButton;
int i = 1;
private int currentIndex = 0;
String question, answer, UserAnswer;
ArrayList<String> QuestionList = new ArrayList<String>();
ArrayList<String> AnswerList = new ArrayList<String>();
private JTextArea playTextAns;
private JTextArea playTextQues;
Scanner sc = new Scanner(System.in);
public ACEQUIZ(){

// Frame settings
Font headline = new Font("Times New Roman", Font.BOLD, 45);
JFrame.setDefaultLookAndFeelDecorated(true);
ACEQUIZ1 = new JFrame("ACEQUIZ");
ACEQUIZ1.setSize(new Dimension(350, 500));
ACEQUIZ1.setResizable(false);
ACEQUIZ1.setLayout(new BorderLayout());

panelsContainer = new JPanel();
panelsContainer.setBackground(Color.CYAN);
panelsContainer.setLayout(new GridLayout(3, 1));
panelsContainer.setBorder(BorderFactory.createEmptyBorder(70, 30, 30, 30)); // Add 10px padding to all sides

// Create the first JPanel for word "WELCOME"
panel1 = new JPanel();
panel1.setBackground(Color.CYAN);
label1 = new JLabel("WELCOME");
panel1.add(label1);
label1.setFont(headline);
panelsContainer.add(panel1);
// Create the second JPanel for word "TO"
panel2 = new JPanel();
panel2.setBackground(Color.CYAN);
label2 = new JLabel("TO");
panel2.add(label2);
label2.setFont(headline);
panelsContainer.add(panel2);

// Create the third JPanel for word "ACEQUIZ"
panel3 = new JPanel();
panel3.setBackground(Color.CYAN);
label3 = new JLabel("ACEQUIZ");
panel3.add(label3);
label3.setFont(headline);
panelsContainer.add(panel3);

ACEQUIZ1.add(panelsContainer, BorderLayout.CENTER);

startButtonPanel = new JPanel();
startButtonPanel.setBackground(Color.CYAN);
startButtonPanel.setLayout(new GridLayout(0, 1));
startButtonPanel.setBorder(BorderFactory.createEmptyBorder(0, 120, 100, 120));

startButton = new JButton("Start");
startButtonPanel.add(startButton);
startButtonPanel.setFocusable(true);

ACEQUIZ1.add(startButtonPanel, BorderLayout.SOUTH);

ACEQUIZ1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ACEQUIZ1.setLocationRelativeTo(null);
ACEQUIZ1.setVisible(true);
ACEQUIZ1.validate();

startButton.addActionListener(new ActionListener() {
Override
public void actionPerformed(ActionEvent e){
ACEQUIZ1.dispose();
SecondFrame = new JFrame("ACEQUIZ");
SecondFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SecondFrame.setSize(350, 500);

secondMainPanel = new JPanel();
secondMainPanel.setBackground(Color.CYAN);
Font QandAfont = new Font("Times New Roman", Font.BOLD, 14);

QandApanel = new JPanel();
QandApanel.setBackground(Color.CYAN);
QandApanel.setLayout(new BoxLayout(QandApanel, BoxLayout.Y_AXIS));

quesLabel = new JLabel(i + ".Enter Question:");
quesLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
quesLabel.setFont(QandAfont);
JTextArea textques = new JTextArea(3, 22);
textques.setLineWrap(true);
textques.setWrapStyleWord(true);


ansLabel = new JLabel(i + ".Enter a Answer:");
ansLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
ansLabel.setFont(QandAfont);
JTextArea textans = new JTextArea(3, 22);
textans.setLineWrap(true);
textans.setWrapStyleWord(true);

spacer1 = new JLabel(" ");
spacer2 = new JLabel(" ");

// Add the labels and text areas to the panel
QandApanel.add(quesLabel);
QandApanel.add(textques);
QandApanel.add(ansLabel);
QandApanel.add(textans);
QandApanel.add(spacer1);
QandApanel.add(spacer2);

// Create the "Add" and "Play" button
JPanel panelButton = new JPanel(new FlowLayout());
panelButton.setBackground(Color.CYAN);

addButton = new JButton("Add");
addButton.setFocusable(true);
addButton.setPreferredSize(new Dimension(80, 25));

playButton = new JButton("Play");
playButton.setFocusable(true);
playButton.setPreferredSize(new Dimension(80, 30));

panelButton.add(addButton);
panelButton.add(playButton);


QandApanel.add(panelButton);
secondMainPanel.add(QandApanel);

JScrollPane scrollPane = new JScrollPane(secondMainPanel); // Wrap the main panel with a JScrollPane
SecondFrame.add(scrollPane);

SecondFrame.setLocationRelativeTo(null);
SecondFrame.setResizable(false);
SecondFrame.setVisible(true);
SecondFrame.repaint();
SecondFrame.revalidate();

addButton.addActionListener(new ActionListener (){
Override
public void actionPerformed(ActionEvent f){

i++;

question = textques.getText();
answer = textans.getText();
QuestionList.add(question);
AnswerList.add(answer);

quesLabel = new JLabel(i + ".Enter Question:");
quesLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
quesLabel.setFont(QandAfont);
JTextArea textques = new JTextArea(3, 22);
textques.setLineWrap(true);
textques.setWrapStyleWord(true);

ansLabel = new JLabel(i + ".Enter a Answer:");
ansLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
ansLabel.setFont(QandAfont);
JTextArea textans = new JTextArea(3, 22);
textans.setLineWrap(true);
textans.setWrapStyleWord(true);

spacer1 = new JLabel(" ");
spacer2 = new JLabel(" ");

QandApanel.add(quesLabel);
QandApanel.add(textques);
QandApanel.add(ansLabel);
QandApanel.add(textans);
QandApanel.add(spacer1);
QandApanel.add(spacer2);

QandApanel.remove(panelButton);
QandApanel.add(panelButton);

SecondFrame.revalidate();
SecondFrame.repaint();
SecondFrame.validate();

}
});
playButton.addActionListener(new ActionListener() {
Override
public void actionPerformed(ActionEvent g) {
question = textques.getText();
answer = textans.getText();
QuestionList.add(question);
AnswerList.add(answer);
SecondFrame.dispose();

ThirdFrame = new JFrame("ACEQUIZ");
ThirdFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ThirdFrame.setSize(350, 500);
ThirdFrame.setLocationRelativeTo(null);
ThirdFrame.setResizable(false);
ThirdFrame.setVisible(true);

ThirdMainPanel = new JPanel();
ThirdMainPanel.setBackground(Color.CYAN);
ThirdMainPanel.setBorder(BorderFactory.createEmptyBorder(110, 0, 0, 0));

playQandApanel = new JPanel();
playQandApanel.setLayout(new BoxLayout(playQandApanel, BoxLayout.Y_AXIS));
playQandApanel.setBackground(Color.CYAN);

playQuesLabel = new JLabel("Question: ");
playQuesLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
playQuesLabel.setFont(QandAfont);
playTextQues = new JTextArea(3, 30);
playTextQues.setLineWrap(true);
playTextQues.setWrapStyleWord(true);
playTextQues.setText(QuestionList.get(currentIndex));
JScrollPane scrollPane = new JScrollPane(playTextQues);
playTextQues.setEditable(false);

playAnsLabel = new JLabel("Enter a Answer Here: ");
playAnsLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
playAnsLabel.setFont(QandAfont);
playTextAns = new JTextArea(3, 30);
playTextAns.setLineWrap(true);
playTextAns.setWrapStyleWord(true);

playQandApanel.add(playQuesLabel);
playQandApanel.add(playTextQues);
playQandApanel.add(playAnsLabel);
playQandApanel.add(playTextAns);

JPanel nextButtonPanel = new JPanel();
nextButtonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
nextButtonPanel.setBackground(Color.CYAN);

nextButton = new JButton("Next");
nextButton.setFocusable(true);
nextButton.setPreferredSize(new Dimension(80, 30));
nextButtonPanel.add(nextButton);

playQandApanel.add(nextButtonPanel);
ThirdMainPanel.add(playQandApanel);
ThirdFrame.add(ThirdMainPanel);

ThirdFrame.revalidate();
ThirdFrame.repaint();
ThirdFrame.validate();
nextButton.addActionListener((ActionEvent h) -> {
if (currentIndex < QuestionList.size() - 1) {
currentIndex++; // Increment the current index

// Get the next question and update the JTextArea
String nextQuestion = QuestionList.get(currentIndex);
playTextQues.setText(nextQuestion);
} else {
// No more questions, display a message or perform any other desired action
JOptionPane.showMessageDialog(null, "No more questions.");
}
});

}
});

}
});
}

public static void main(String[] args){
new ACEQUIZ();

}

}

 
Based on your code, it seems that you are not updating the value of the currentIndex variable after iterating through your QuestionList array. This is why the same question is being displayed even if you click the Next button.

To fix this issue, you can update the value of the currentIndex variable every time the Next button is clicked. Here's how you can modify your nextButton.addActionListener() method:

Java:
nextButton.addActionListener((ActionEvent h) -> {
    if (currentIndex < QuestionList.size() - 1) {
        currentIndex++; // Increment the current index
        
        // Get the next question and update the JTextArea
        String nextQuestion = QuestionList.get(currentIndex);
        playTextQues.setText(nextQuestion);
    } else {
        // No more questions, display a message or perform any other desired action
        JOptionPane.showMessageDialog(null, "No more questions.");
    }
});

In this updated code, we first check if there are still questions left to display by comparing the value of currentIndex with the size of the QuestionList array. If there are still questions left, we increment the value of currentIndex and get the next question from the QuestionList array using QuestionList.get(currentIndex). Finally, we update the text of the playTextQues JTextArea with the next question.

If there are no more questions left to display, we can display a message to the user using a JOptionPane. You can modify this as per your requirement.
 
Status
Not open for further replies.

About this Thread

  • 1
    Replies
  • 387
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
1,139
Guests online
1,265
Total visitors
2,404

Forum statistics

Threads
2,273,429
Posts
28,949,439
Members
1,235,743
Latest member
lilwar14
Back
Top