2015-06-22 2 views
0

Итак, у меня есть следующий код, чтобы сделать программу викторины. Код, который я использую, расширяет JDialog. Мне было интересно, есть ли способ конвертировать его в JPanel, поскольку, когда я пытаюсь запустить код, окно все мало, и я не могу переместить завершенную кнопку рядом с кнопкой «Далее». Также, если я хочу, чтобы вопрос был случайным, как это сделать?Преобразование JDialog в Jframe/JPanel

Спасибо

import javax.swing.*; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import java.util.Random; 
import javax.swing.JRadioButton; 
import java.util.ArrayList; 
import java.util.Enumeration; 
import java.util.List; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class MultipleChoice extends JDialog { 
int correctAns; 
List<Question> questions = new ArrayList<Question>(); 
//asnwers 
JRadioButton[] responses; 
ButtonGroup group = new ButtonGroup(); 
//bottom 
JButton next = new JButton("Next"); 
JButton finish = new JButton("Finish"); 

public MultipleChoice(){ 
    super(); 
    setModal(true); 
    setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS)); //setting up boxlayout 

    questions.add(new Question("Which of the following is NOT one of the 5 great lakes?",new String[]{"Lake Mead","Lake Huron","Lake Michigan","Lake Erie"}, "Lake Mead")); 
    questions.add(new Question("What is the Capital of Colorado?",new String[]{"Boulder","Aspen","Denver","Cheyenne"},"Denver")); 
    questions.add(new Question("Each side of a baseball diamond is 90 feet in length. How far is it around the Baseball Diamond?",new String[]{"270","360","180","390"},"360")); 
    questions.add(new Question("What color do you get when you combine an equal amount of red paint with an equal amount of yellow paint?",new String[]{"Blue","Orange","Green","Pink"},"Orange")); 
    questions.add(new Question("How many sides does a trapezoid have?",new String[]{"3","4","5","6"},"2")); 
    questions.add(new Question("Which is a simile?",new String[]{"My sister is cute like a bunny","My mom has a manly snore","My dad is better than yours"},"My sister is cute like a bunny")); 
    questions.add(new Question("Polar bears eat penquins?",new String[]{"True","False"},"True")); 

    //bottom 
    next.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent ae){ 
      setVisible(false); 
     } 
    }); 
    finish.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent ae){ 
      setVisible(false); 
     } 
    }); 
    } 

public int beginQuiz(){ 
     int score=0; 
     for (Question q : questions){ 
      displayQuestion(q); 
      if (group.getSelection().getActionCommand().equals(q.getans())){ 
       score++; 
      } 
     } 
     dispose(); 
     return score; 
} 

private void displayQuestion(Question q){ 
     getContentPane().removeAll(); 
     for (Enumeration buttonGroup=group.getElements(); buttonGroup.hasMoreElements();){ 
      group.remove((AbstractButton)buttonGroup.nextElement()); 
     } 

     JLabel questionText = new JLabel(q.getquestion()); 
     getContentPane().add(questionText); 
     for (String answer : q.getanswers()){ 
      JRadioButton radio = new JRadioButton(answer); 
      radio.setActionCommand(answer); 
      group.add(radio); 
      getContentPane().add(radio); 
     } 
     getContentPane().add(next); 
     getContentPane().add(finish); 
     pack(); 
     setVisible(true); 
    } 

public static void showSummary(int score){ 
    JOptionPane.showMessageDialog(null,"All Done :), here are your results"+ 
     "\nNumber of incorrect Answers: \t"+(7-score)+ 
     "\nNumber of Correct Answers: \t"+(score)+ 
     "\nPercent Correct: \t\t"+(int)(((float)(score)/7)*100)+"%" 
    ); 
    if (((int)(((float)(score)/7)*100))> 0.6) 
     JOptionPane.showMessageDialog(null,"You Passed!"); 
    else { 
     JOptionPane.showMessageDialog(null,"You are NOT Smarter than a 5th Grader"); 
     } 
} 

public static void main(String[] args){ 
    MultipleChoice quiz = new MultipleChoice(); 
    int score = quiz.beginQuiz(); 
    showSummary(score); 
} 
} 

class Question { 
private String question; 
private String[] answers; 
private String ans; 

String getquestion(){ //getter 
    return question; 
} 
void setquestion(String str){ //setter 
    question = str; 
} 
String[] getanswers(){ //getter 
    return answers; 
} 
void setanswers(String[] str2){ //setter 
    answers = str2; 
} 
String getans(){ //getter 
    return ans; 
} 
void setans(String str3){ //setter 
    ans = str3; 
} 

public Question(String possibleQuestion ,String[] possibleAnswer , String correctAnswer){  //constructor 
    question = possibleQuestion; 
    answers = possibleAnswer; 
    ans = correctAnswer; 
} 
} 

Обновлен с новыми правками

После того, как я отредактировал и попытался превратить его в JPanel и использовать CardLayout, как предложил я столкнулся несколько ошибок и не может показаться чтобы выяснить, как получить String [] possibleanswer и string correctanswer для отображения, чтобы система узнала и соответствовала правильному ответу. вот мой обновленный код

Спасибо

import javax.swing.*; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import java.util.Random; 
import java.awt.CardLayout; 
import javax.swing.JRadioButton; 
import java.util.ArrayList; 
import java.util.Enumeration; 
import java.util.List; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class MultipleChoice { 
int correctAns; 
List<Question> questions = new ArrayList<Question>(); 
JPanel p=new JPanel(); 
CardLayout cards=new CardLayout(); 
int numQs; 
int wrongs=0; 
int total=0; 

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

public MultipleChoice(){ 
    JFrame frame = new JFrame("Are you Smarter than a 5th Grader?"); 
    frame.setResizable(true); 
    frame.setSize(500,400); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    questions.add(new Question("Which of the following is NOT one of the 5 great lakes?",new String[]{"Lake Mead","Lake Huron","Lake Michigan","Lake Erie"}, "Lake Mead")); 
    questions.add(new Question("What is the Capital of Colorado?",new String[]{"Boulder","Aspen","Denver","Cheyenne"},"Denver")); 
    questions.add(new Question("Each side of a baseball diamond is 90 feet in length. How far is it around the Baseball Diamond?",new String[]{"270","360","180","390"},"360")); 
    questions.add(new Question("What color do you get when you combine an equal amount of red paint with an equal amount of yellow paint?",new String[]{"Blue","Orange","Green","Pink"},"Orange")); 
    questions.add(new Question("How many sides does a trapezoid have?",new String[]{"3","4","5","6"},"2")); 
    questions.add(new Question("Which is a simile?",new String[]{"My sister is cute like a bunny","My mom has a manly snore","My dad is better than yours"},"My sister is cute like a bunny")); 
    questions.add(new Question("Polar bears eat penquins?",new String[]{"True","False"},"True")); 

    p.setLayout(cards); 
    numQs=questions.size(); 
    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); 
    frame.add(p); 
    frame.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); 
    } 
} 

public void showSummary(){ 
    JOptionPane.showMessageDialog(null,"All Done :), here are your results"+ 
     "\nNumber of incorrect Answers: \t"+wrongs+ 
     "\nNumber of Correct Answers: \t"+(total-wrongs)+ 
     "\nPercent Correct: \t\t"+(int)(((float)(total-wrongs)/total)*100)+"%" 
    ); 
    if (((int)(((float)(total-wrongs)/total)*100))> 0.6) 
     JOptionPane.showMessageDialog(null,"You Passed!"); 
    else { 
     JOptionPane.showMessageDialog(null,"You are NOT Smarter than a 5th Grader"); 
     } 
    System.exit(0); 
    }  
} 

class Question extends JPanel { 
int correctAns; 
MultipleChoice Choices; 
int selected; 
boolean used; 
//questions 
JPanel qPanel=new JPanel(); 
//answers 
JPanel aPanel=new JPanel(); 
JRadioButton[] responses; 
//bottom 
JPanel botPanel=new JPanel(); 
JButton next=new JButton("Next"); 
JButton finish=new JButton("Finish"); 

private String question; 
private String[] answers; 
private String ans; 
public Question(String possibleQuestion ,String[] possibleAnswer , String correctAnswer){  //constructor 
    this.Choices=Choices; 
    question = possibleQuestion; 
    answers = possibleAnswer; 
    ans = correctAnswer; 

    setLayout(new BoxLayout(this,BoxLayout.Y_AXIS)); 
    //question 
    qPanel.add(new JLabel(question)); 
    add(qPanel); 
    //answer 
    responses=new JRadioButton[answers.length]; 
    //display possible answer and answers 
    aPanel.add(responses[i]); 

    add(aPanel); 


    //bottom 
    next.addActionListener(this); 
    finish.addActionListener(this); 
    botPanel.add(next); 
    botPanel.add(finish); 
    add(botPanel); 
} 

public Question(String possibleQuestion ,String[] possibleAnswer){ //constructor overloading 
    question = possibleQuestion; 
    answers = possibleAnswer; 
} 

String getquestion(){ //getter 
    return question; 
} 
void setquestion(String str){ //setter 
    question = str; 
} 
String[] getanswers(){ //getter 
    return answers; 
} 
void setanswers(String[] str2){ //setter 
    answers = str2; 
} 
String getans(){ //getter 
    return ans; 
} 
void setans(String str3){ //setter 
    ans = str3; 
} 

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

Новые Ревизии Так после создания интерфейса и его реализации. Я как-то не могу показать вопрос или варианты. JButton, похоже, не работает.

import java.awt.BorderLayout; 
import java.awt.CardLayout; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collections; 
import java.util.List; 
import javax.swing.ButtonGroup; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.EmptyBorder; 

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

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

public MultipleChoice(){ 
    List<Question> questions = new ArrayList<>(); 

    questions.add(new Question("Which of the following is NOT one of the 5 great lakes?",new String[]{"Lake Mead","Lake Huron","Lake Michigan","Lake Erie"}, "Lake Mead")); 
    questions.add(new Question("What is the Capital of Colorado?",new String[]{"Boulder","Aspen","Denver","Cheyenne"},"Denver")); 
    questions.add(new Question("Each side of a baseball diamond is 90 feet in length. How far is it around the Baseball Diamond?",new String[]{"270","360","180","390"},"360")); 
    questions.add(new Question("What color do you get when you combine an equal amount of red paint with an equal amount of yellow paint?",new String[]{"Blue","Orange","Green","Pink"},"Orange")); 
    questions.add(new Question("How many sides does a trapezoid have?",new String[]{"3","4","5","6"},"2")); 
    questions.add(new Question("Which is a simile?",new String[]{"My sister is cute like a bunny","My mom has a manly snore","My dad is better than yours"},"My sister is cute like a bunny")); 
    questions.add(new Question("Polar bears eat penquins?",new String[]{"True","False"},"True")); 

    JFrame frame = new JFrame("Are you Smarter than a 5th Grader?"); 
    frame.setResizable(true); 
    frame.setSize(500,400); 
    frame.add(new QuizPane(questions)); 
    frame.pack(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 

    } 
public class QuizPane extends JPanel { 
    private CardLayout cardLayout; 
    private int currentQuestion; 
    private JButton next; 
    private List<Question> question; 

    private JPanel panelOfQuestions; 
    public QuizPane(List<Question> question) { 
     this.question = question; 
     cardLayout = new CardLayout(); 
     panelOfQuestions = new JPanel(cardLayout); 

     JButton start = new JButton("Start"); 
     start.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       currentQuestion = -1; 
       next(); 
      } 
     }); 

     JPanel filler = new JPanel(new GridBagLayout()); 
     filler.add(start); 
     panelOfQuestions.add(filler, "start"); 

     for (int index = 0; index < question.size(); index++) { 
      QuestionInterface quiz = question.get(index); 
      QuestionPane pane = new QuestionPane(quiz); 
      panelOfQuestions.add(pane, Integer.toString(index)); 
     } 
     panelOfQuestions.add(new JLabel("The quiz is over"), "last"); 
     currentQuestion = 0; 
     cardLayout.show(panelOfQuestions, "start"); 

     setLayout(new BorderLayout()); 
     add(panelOfQuestions); 

     JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
     next = new JButton("Next"); 
     buttonPane.add(next); 
     next.setEnabled(false); 

     add(buttonPane, BorderLayout.SOUTH); 

     next.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       next(); 
      } 
     }); 
    } 
    public void next(){ 
     if (currentQuestion >= question.size()) { 
      cardLayout.show(panelOfQuestions, "last"); 
      next.setEnabled(false); 
      // You could could loop through all the questions and tally 
      // the correct answers here 
      } 
     else { 
      cardLayout.show(panelOfQuestions, Integer.toString(currentQuestion)); 
      next.setText("Next"); 
      next.setEnabled(true); 
     } 
    } 

    /*public void showSummary(){ // try to get to show summary after the quiz 
    JOptionPane.showMessageDialog(null,"All Done :), here are your results"+ 
     "\nNumber of incorrect Answers: \t"+wrongs+ 
     "\nNumber of Correct Answers: \t"+(total-wrongs)+ 
     "\nPercent Correct: \t\t"+(int)(((float)(total-wrongs)/total)*100)+"%" 
    ); 
    if (((int)(((float)(total-wrongs)/total)*100))> 0.6) 
     JOptionPane.showMessageDialog(null,"You Passed!"); 
    else { 
     JOptionPane.showMessageDialog(null,"You are NOT Smarter than a 5th Grader"); 
     } 
    System.exit(0); 
    }*/ 

    } 

    public interface QuestionInterface { 

    public String getquestion(); 

    public String[] getOptions(); 

    public String getAnswer(); 

    public String getUserResponse(); 

    public void setUserResponse(String response); 

    public boolean isCorrect(); 
    } 
    public class Question implements QuestionInterface { 
private String possibleQuestion; 
private String[] Options; 
public String correctAnswer; 

private String userResponse; 

public Question(String possibleQuestion ,String[] Options , String correctAnswer){  //constructor 
    this.possibleQuestion = possibleQuestion; 
    this.Options = Options; 
    this.correctAnswer = correctAnswer; 
} 

public Question(String possibleQuestion ,String[] possibleAnswer){ //constructor overloading 
    this.possibleQuestion = possibleQuestion; 
    this.Options = Options; 
    } 

public String getquestion(){ //getter 
    return possibleQuestion; 
    } 

void setquestion(String str){ //setter 
    possibleQuestion = str; 
    } 

public String[] getOptions(){ //getter 
    return Options; 
} 

void setanswers(String[] str2){ //setter 
    Options = str2; 
} 

public String getAnswer(){ //getter 
    return correctAnswer; 
} 

void setans(String str3){ //setter 
    correctAnswer = str3; 
} 

public String getUserResponse() { 
     return userResponse; 
    } 

public void setUserResponse(String response) { 
     userResponse = response; 
    } 

public boolean isCorrect() { 
     return getAnswer().equals(getUserResponse()); 
    } 
} 
public class QuestionPane extends JPanel { 

    private QuestionInterface question; 

    public QuestionPane(QuestionInterface question) { 
     this.question = question; 

     setLayout(new BorderLayout()); 

     JLabel prompt = new JLabel("<html><b>" + question.getquestion() + "</b></html>"); 
     prompt.setHorizontalAlignment(JLabel.LEFT); 

     add(prompt, BorderLayout.NORTH); 

     JPanel guesses = new JPanel(new GridBagLayout()); 
     guesses.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridwidth = GridBagConstraints.REMAINDER; 
     gbc.weightx = 1; 
     gbc.anchor = GridBagConstraints.WEST; 

     List<String> options = new ArrayList<>(Arrays.asList(question.getOptions())); 
     options.add(question.getAnswer()); 
     Collections.sort(options); 

     ButtonGroup bg = new ButtonGroup(); 
     for (String option : options) { 
      JRadioButton btn = new JRadioButton(option); 
      bg.add(btn); 

      guesses.add(btn, gbc); 
     } 

     add(guesses); 

    } 

    public QuestionInterface getQuestion() { 
     return question; 
    } 

    public class ActionHandler implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 
      getQuestion().setUserResponse(e.getActionCommand()); 
     } 

     } 

    } 
} 

ответ

3

код я использую расширяет JDialog. Мне было интересно, если есть способ, чтобы преобразовать его в JPanel

Добро пожаловать в удивительный мир, почему вы не должны простираться от контейнеров верхнего уровня (или, по меньшей мере, одной из причин)

Start путем расширения MultipleChoice от JPanel вместо JDialog. Это произведет ряд ошибок компилятора ...

  • setModal(true); Вид бессмысленно на панели, так что вы можете избавиться от него
  • getContentPane(), хорошо, панель не имеет JRootPane так Безразлично У вас есть область содержимого. Вы можете использовать this вместо
  • dispose. Ну, панель не окно, поэтому она не может быть «утилизирована» для каждого. Я лично заменил бы его каким-то Observer Pattern, который предоставил бы уведомления, когда состояние викторины изменилось (например, оно было завершено), чтобы вызывающий мог справиться с ним так, как они хотят. Дело в том, что не делайте предположений о том, как люди могут использовать ваш компонент.
  • pack, ну, это идет в том же лодке, что и другие, поэтому вы можете просто избавиться от него.
  • setVisible, хорошо, компоненты Swing видны по умолчанию, поэтому, вероятно, это немного бессмысленно.

Знаешь, все, что вам нужно сделать, это создать экземпляр вашего MultipleChoice панели и добавить его к тому, что когда-либо контейнер вы хотите

с тех пор когда я пытаюсь запустить код окна на все малые и я не могу переместить завершенную кнопку рядом с кнопкой «Далее»

Это вопрос макета. В вашем случае, я хотел бы использовать схему соединения, может быть, с помощью FlowLayout для кнопок и BorderLayout для «вопросы» и панель кнопок (в SOUTH положении)

Кстати, вы, вероятно, следует с помощью CardLayout, чтобы показать вопросы, которые I presented in this surprisingly similar question

Также, если я хочу, чтобы вопрос был случайным, как это сделать?

Collections.shuffle(questions);, возможно, самое легкое решение, о котором я могу думать. Как только вы его назовете, List будет случайным образом перетасоваться.

поэтому я просто редактирую код, мне было интересно, можете ли вы дать некоторые комментарии, как исправить это. спасибо

Ну, там идет контекст оригинального ответа ...

  • Взгляните на Collections Trail, вы используете List, как если бы это был массив, который теперь, как они Работа.
  • Question (JPanel) не реализует ActionListener так next.addActionListener(this); не собирается работать
  • Вы забыли завернуть aPanel.add(responses[i]); в петлю и создать JRadioButton с, что все должны быть добавлены к тому же ButtonGroup
  • Choices in is Question никогда не присваивал значение и будет генерировать NullPointerException. Я был бы обеспокоен возможностью разоблачения MultipleChoice и лично использовал бы interface, который предоставил договор о том, что может быть выполнено против класса
  • При добавлении ваших «возможных» ответов вам также необходимо указать правильный ответ. Я хотел бы добавить Постройте List из JRadioButton, один для каждого возможного ответа, и один за правильный ответ, я бы использовать Collection.shuffle перетасовать эту List в какой-то случайным образом, а затем добавить кнопки на панели Question
+0

так я просто редактирую код, мне было интересно, можете ли вы дать некоторые комментарии, как его исправить. спасибо –

+0

Отлично, теперь контекст вопроса не соответствует контексту ответа: P – MadProgrammer

+0

Я смотрел ваш пример CardLayout, и я чувствую, что он похож на аналогичную программу, которую я искал. поэтому я пытался реализовать некоторые вещи из этого кода за –

Смежные вопросы