-1

I cannot for the life of me figure out how to place these buttons i have, the Next and Finish buttons that are supposed to be at the bottom of every page, if anyone can tell me how i can get these buttons working that would be awesome because this is a project due on TUESDAY. So. Yeah.

Its a Quiz or Trivia game that, in the end, will have 120 questions that i am creating using a CardBox Layout, that way i can just keep adding them without having to have integers for each one.

I uploaded the Eclipse Luna project to DropBox: https://www.dropbox.com/s/ihyj48xhghb22zd/QuizGame.zip?dl=0

and here is the code...

RadioQuestion.java

package GameStructure;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.BoxLayout;


public class RadioQuestion extends JPanel implements ActionListener{
int correctAns;
int selected;
//questions
JPanel qPanel=new JPanel();
//answers
JPanel aPanel=new JPanel();
JRadioButton[] responses;
ButtonGroup group=new ButtonGroup();
//bottom
JPanel botPanel=new JPanel();
static JButton next=new JButton("Next");
JButton finish=new JButton("Finish");
private Quiz quiz;
public boolean used;

public static void main (String args[]){
    JFrame frame=new JFrame("WWII Quiz Review Game");
    frame.setSize(400,300);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.setResizable(true);

    String[] answers={"wrong1","right","wrong2"};
    frame.add(new RadioQuestion("what's right?", answers,1));

    frame.setVisible(true);

}

public RadioQuestion(String q, String[] options, int ans, Quiz quiz){
    this.quiz=quiz;
    setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
    correctAns=ans;
    //question
    qPanel.add(new JLabel(q));
    add(qPanel);
    //answer
    responses=new JRadioButton[options.length];
    for(int i=0;i<options.length;i++){
        responses[i]=new JRadioButton(options[i]);
        responses[i].addActionListener(this);
        group.add(responses[i]);
        aPanel.add(responses[i]);
    }
    add(aPanel);
    //bottom
    next.addActionListener(this);
    finish.addActionListener(this);
    botPanel.add(next);
    botPanel.add(finish);

    }

public RadioQuestion(String string, String[] answers, int i) {
    // TODO Auto-generated constructor stub
}


public void actionPerformed(ActionEvent e) {
    Object src=e.getSource();
    //next button
    if(src.equals(next)){
        showResult();
        if(selected==correctAns){
            used=true;
            quiz.next();
        }
    }
    //finish button
    if(src.equals(finish)){

    }
}

private void showResult() {
    String text=responses[selected].getText();
    quiz.total++;
    if(selected==correctAns){
        JOptionPane.showMessageDialog(null, text+ " is the correct answer\nWell done!");
    }else{
        quiz.wrongs++;
        JOptionPane.showMessageDialog(null, text+" is wrong\n Sorry");
    }

}

}

Then the main class that i'll be running...

Quiz.java

package GameStructure;
import javax.swing.*;

import java.awt.CardLayout;
import java.awt.Component;
import java.util.Random;

import javax.swing.JOptionPane;


public class Quiz extends JFrame{
JPanel p=new JPanel();
CardLayout cards=new CardLayout();
int numQs;
int wrongs=0;
int total=0;


//place answers here
String[][] answers={

    {"1","2","4","5"},
    {"1","2","4","5"},

};

//place questions here
RadioQuestion questions[]={

    new RadioQuestion("What is the number 1", answers[0],1,this),
    new RadioQuestion("What is the number 2", answers[0],2,this),


};

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


public Quiz(){
super("Quiz Game");
setResizable(true);
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);

p.setLayout(cards); 
numQs=questions.length;
for(int i=0;i<numQs;i++){
    p.add(questions[i],"q"+i);
}
Random r=new Random();
int i=r.nextInt(numQs);
cards.show(p, "q"+i);
add(p);
setVisible(true);
}

public void next() {
if((total-wrongs)==numQs){
    showSummary();
}else{
    Random r=new Random();
    boolean found=false;
    int i=0;
    while(!found){
        i=r.nextInt(numQs);
        if(!questions[i].used){
            found=true;
        }

    }
    cards.show(p, "q"+i);
}

}


private void showSummary() {
JOptionPane.showMessageDialog(null, "All Done!, here are your results"+"\nNumber of incorrect Answers: \t"+wrongs+
        "\nNumber of correct answers: \t"+(total-wrongs)+
        "\nAverage Incorrect Answers per Question: \t"+((float)wrongs/numQs)+
        "\nPercent Correct: \t\t"+(int)(((float)(total-wrongs)/total)*100)+"%");
            System.exit(0);


}
}
  • 1
    `package GameStructure;` a package name should (by naming conventions) be `alllowercase` which kind of encourages a single word name for the package(s) e.g. `package game.structure;` – Andrew Thompson Mar 09 '15 at 09:20

1 Answers1

4

You are forgetting to add your botPanel

add(botPanel);
badfilms
  • 4,317
  • 1
  • 18
  • 31
  • Just as you have done with the rest of the panels you added, you will need to make sure that you add the botPanel. If you look through your RadioQuestion class, you will notice that for each of your three sections, //questions, //answers, and //bottom -- you have a corresponding panel. Except that in the //bottom section, unlike the other two, you did not add your panel. Without this, your buttons will not appear. – badfilms Mar 09 '15 at 04:26
  • 1
    Thank you so much. I would have figured that out on my own but I was out of the house. You've been very helpful! –  Mar 09 '15 at 04:28
  • So i was able to implement the buttons into the code and have them show up but the Next button only showed up one launch and then disappeared the next time i ran it even though i hadn't changed anything. Also, the next and finish button aren't doing their functions they just tell me that i got all the answers wrong. Ontop of that, now when i select the answer it says i selected another one....the code is here. Quiz.java: http://pastebin.com/J4imJGu2 RadioQuestion.java: http://pastebin.com/z1qAtEgg –  Mar 09 '15 at 23:41