Help closing frames and disabling frames

Advertisement
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) {
this.parentFrame.close(); 
}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 :)

Advertisement

Replay

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();
comp.setBorder(BorderFactory.createEtchedBorder());
questionPanel.add(comp);
JButton checkAnswersBtn = new JButton("CheckAnswers");
checkAnswersBtn.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
int score = 0;
for (QuestionGUI quest : questionGuis)
if (quest.isSelectionCorrect())
score++;
else
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();
btnPanel.add(checkAnswersBtn);
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());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
public static void main(String[] args)
java.awt.EventQueue.invokeLater(new Runnable()
public void run()
createAndShowUI();
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);
rButton.setActionCommand(str);
radioPanel.add(rButton);
buttonGrp.add(rButton);
mainPanel.setLayout(new BorderLayout(10, 10));
mainPanel.add(new JLabel(question.getQuestion(), SwingConstants.LEFT),
BorderLayout.NORTH);
mainPanel.add(radioPanel, BorderLayout.CENTER);
public Question getQuestion()
return question;
public String getSelectedString()
ButtonModel model = buttonGrp.getSelection();
if (model != null)
return model.getActionCommand();
else
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)
answers.add(string);
answers.add(answer);
Collections.shuffle(answers);
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

Tags:

  1. mac g5 x800 xt linux
  2. fk09 mass confirmation of vendors
  3. Using the load TXT
  4. HRDM
  5. stepa89
  6. новости челябинск мир 74
  7. meangdl
  8. woolymk
  9. spinx1l
  10. S4VP
Copyrights 2019 Fcffair BigData Resource, All rights reserved