无法弄清楚如何使我的按钮显示出来

时间:2015-03-09 00:52:57

标签: java swing jbutton

我不能为我的生活弄清楚如何放置我有这些按钮,Next和Finish按钮应该在每个页面的底部,如果有人可以告诉我如何让这些按钮工作会很棒,因为这是一个在星期二到期的项目。所以。呀。

它是一个测验或琐事游戏,最后将有120个问题,我正在使用CardBox布局创建,这样我就可以继续添加它们,而不必为每个都添加整数。

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

这是代码......

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");
    }

}

}

然后我将要运行的主要课程......

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 个答案:

答案 0 :(得分:4)

您忘记添加botPanel

add(botPanel);