Help closing frames and disabling frames

November 30

hey, i'm making this type of quiz game and i've got the primary game frame with it's game panel. When the player matches two images on this main panel, another frame with a panel is drawn on top of the main game and asks the user a question, if the user gets the right answer i want it to return back to the game and close the quiz frame. I can't use system.exit(0) when the user gets the correct answer because the whole application shuts down. how do i just close this one? Also, i make a new quiz frame object for each match on the game board, the constructor of the quiz frame takes an int as a parameter which represents the game state and thus which question to ask the user.
So that's just a bit of background information, the two questions i have are:
1. How do i close the quiz frame when the user enters the correct answer without closing the entire game application? As it is my Panel within my quiz frame doing the check to see if the users input is correct, i'd like to be able to close the quiz frame down from this panel. so i'm looking for
something like:
this is just some pseudo code to explain what i want to do
if(answerCorrect) {
}2. How do disable the primary game while the quiz frame is up and displaying a question to the player? I don't want the player to be able to continue playing the game without first entering the correct answer. and only when they answer correct do I wish the main game to become re-enabled.
If the answers are not simple then i'd be happy to read any links/information which would help.
Thanks a heap i really appreciate it :)



Encephalopathic wrote:
but a better and more flexible way to implement this is to allow outside objects pass this array into the QuizPanel object, either by its constructor or by a setter method allowing you to use the same QuizPanel class for different data.Ages ago I downloaded an example from these forums which does just that, and it also randomizes the order or the answers. See below -
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
public class QuizGUI
  private JPanel mainPanel = new JPanel();
  private Question[] questions =
    new Question("How many months in a year?", "12", new String[]
      "1", "Several", "100", "Heck if I know?"
    new Question("Who was buried in Grant's Tomb?", "Grant", new String[]
      "Washington", "Jefferson", "Lincoln", "Mickey Mouse"
    new Question("What's the air-speed velocity of a fully ladden swallow",
        "African or European?", new String[]
          "100 mi/hr", "25 mi/hr", "50 mi/hr", "-10 mi/hr"
    new Question("What color was Washington's white horse?", "White",
        new String[]
          "Blue", "Brown", "Chartreuse", "Mauve"
  private QuestionGUI[] questionGuis = new QuestionGUI[questions.length];
  public QuizGUI()
    JPanel questionPanel = new JPanel(new GridLayout(0, 1, 0, 10));
    for (int i = 0; i < questionGuis.length; i++)
      questionGuis[i] = new QuestionGUI(questions);
JComponent comp = questionGuis[i].getComponent();
JButton checkAnswersBtn = new JButton("CheckAnswers");
checkAnswersBtn.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
int score = 0;
for (QuestionGUI quest : questionGuis)
if (quest.isSelectionCorrect())
System.out.println("For the question: \"" + quest.getQuestion().getQuestion() + "\",");
System.out.println("\"" + quest.getSelectedString() + "\" is the wrong answer");
System.out.println("The correct answer is: \"" + quest.getQuestion().getCorrectAnswer() + "\"");
System.out.println("Score: " + score);
JPanel btnPanel = new JPanel();
int ebGap = 10;
mainPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
mainPanel.setLayout(new BorderLayout());
mainPanel.add(questionPanel, BorderLayout.CENTER);
mainPanel.add(btnPanel, BorderLayout.SOUTH);
public JComponent getComponent()
return mainPanel;
private static void createAndShowUI()
JFrame frame = new JFrame("Quiz");
frame.getContentPane().add(new QuizGUI().getComponent());
public static void main(String[] args)
java.awt.EventQueue.invokeLater(new Runnable()
public void run()
class QuestionGUI
private JPanel mainPanel = new JPanel();
private Question question;
private ButtonGroup buttonGrp = new ButtonGroup();
public QuestionGUI(Question question)
this.question = question;
JPanel radioPanel = new JPanel(new GridLayout(1, 0, 10, 0));
for (String str : question.getAnswers())
JRadioButton rButton = new JRadioButton(str);
mainPanel.setLayout(new BorderLayout(10, 10));
mainPanel.add(new JLabel(question.getQuestion(), SwingConstants.LEFT),
mainPanel.add(radioPanel, BorderLayout.CENTER);
public Question getQuestion()
return question;
public String getSelectedString()
ButtonModel model = buttonGrp.getSelection();
if (model != null)
return model.getActionCommand();
return null;
public boolean isSelectionCorrect()
ButtonModel model = buttonGrp.getSelection();
if (model != null)
return question.isCorrect(model.getActionCommand());
return false;
public JComponent getComponent()
return mainPanel;
class Question
private String question;
private String answer;
private List<String> answers = new ArrayList<String>();
public Question(String q, String answer, String[] badAnswers)
question = q;
this.answer = answer;
for (String string : badAnswers)
public String getQuestion()
return question;
public String[] getAnswers()
return answers.toArray(new String[0]);
public String getCorrectAnswer()
return answer;
public boolean isCorrect(String selection)
return answer.equals(selection);

View 15 Replies


  1. 3 G net worksettings of apple l phone
  2. input VAT.xml
  3. apple tv hulu no signal
  4. software to stop pops safari 5.1.10
  5. download intex cloud fx pdf opener
  6. error: err_wis_30270 how to restore file
  7. itunes login createSession
  8. lsmw RV14BTCI
  9. using outline load utility
  10. supposex7v
Copyrights 2019 Fcffair BigData Resource, All rights reserved