简体   繁体   English

无法弄清楚如何显示我的JButton

[英]Cant figure out how to make my JButtons show up

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. 它的测验或琐事游戏最终将有120个问题,这些问题是我使用CardBox布局创建的,这样,我可以继续添加它们而不必为每个问题添加整数。

I uploaded the Eclipse Luna project to DropBox: https://www.dropbox.com/s/ihyj48xhghb22zd/QuizGame.zip?dl=0 我将Eclipse Luna项目上传到DropBox: https ://www.dropbox.com/s/ihyj48xhghb22zd/QuizGame.zip ? dl =0

and here is the code... 这是代码...

RadioQuestion.java 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 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);


}
}

您忘记添加botPanel

add(botPanel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM