2016-10-11 7 views
0

JFrameに8つの数学問題を追加したいと思います。すべてが 進行中の仕事ですが、ここで役に立つのは私のclass QuizQuestionです。JFrameに複数のパネルを追加するには?

public class QuizQuestion { 
    private String question; 
    private int answer; 

    public QuizQuestion(){ 
     question = "5 + 3"; 
     answer = 5 + 3; 
    } 
    public String getQuestion() { 
     return question; 
    } 

    public int getAnswer() { 
     return answer; 
    } 

} 

はここに私のメインクラスです。これはもちろん問題がどこにあるかです。私は 8つの数学の問題を作成しています(今の問題はすべて同じです)。 8つの問題をすべて同じJFrameに取り込みたいと思っています。私は ループを使用すると、私はJFrameを作成してからループ をTryクラスまでループすると思ったが、うまくいきませんでした。だから私はどのようにすれば これを行うだろうか?

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 
import java.awt.TextField; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 

import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class Try extends JPanel { 
    private JLabel display;   // this is used to display output to the user 
    private JTextField answerInput; // the box where the user enters his answer 
    private JButton button;   // a "Submit" button that the user clicks to submit answer 
    private QuizQuestion question; // the current math question that the user has to answer 
    private long startTime = 0; 
    private long endTime; 
    private int correctFirstTry;  // number of questions answered correctly by the user on the first try 
    private int correctSecondTry; // number of questions answered correctly by the user on the second try 
    private int incorrect;   // number of questions not answered correctly after two tries 
    private final static int STARTING = 0; // represents the "state" before any questions have been asked 
    private final static int FIRST_TRY = 1; // the state while waiting for the first answer to a question 
    private final static int SECOND_TRY = 2; // the state while waiting for the second answer to a question 

    private int state = STARTING; 

    public Try(){ 
     setLayout(new BorderLayout()); 
     display = new JLabel("Question"); 
     display.setPreferredSize(new Dimension(500, 50)); 
     display.setHorizontalAlignment(JLabel.CENTER); 
     display.setVerticalAlignment(JLabel.CENTER); 
     add(display,BorderLayout.CENTER); 
     JPanel bottom = new JPanel(); 
     bottom.add(new JLabel("Enter Answer Here: ")); 
     answerInput = new JTextField(4); 
     answerInput.setEnabled(false); 
     bottom.add(answerInput); 
     button = new JButton("Show Question"); 
     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       buttonPressed(); 
      } 
     }); 
     bottom.add(button); 
     add(bottom, BorderLayout.SOUTH); 
    } 
    private void buttonPressed() { 

     if (state == STARTING) { // Create and display the first question; change state to FIRST_TRY. 
      question = new QuizQuestion(); 
      this.startTime = System.currentTimeMillis()/1000; 
      display.setText("First Question" + "\t" + question.getQuestion()); 
      answerInput.setEnabled(true); 
      answerInput.requestFocus(); 
      button.setText("Submit"); 
      state = FIRST_TRY; 
      return; 
     } 

     String userInput = answerInput.getText().trim(); // Get user's input from the input box. 

     if (userInput.length() == 0) { // Nothing was entered in the box; give an appropriate error message. 
      errorMessage("Enter your answer in the input box,\nand then click the Submit button."); 
      return; 
     } 

     int userAnswer; // user's answer as an integer value 
     try { 
      userAnswer = Integer.parseInt(userInput); // convert input string to integer 
     } 
     catch (NumberFormatException e) { // Input was not valid; give an appropriate error message. 
      errorMessage("\"" + userInput + "\" is not a legal integer.\nPlease enter your answer as an integer."); 
      return; 
     } 

     // At this point, we have the user's answer stored in the int variable userAnswer, and the 
     // state is FIRST_TRY or SECOND_TRY. Check the answer and respond accordingly. 

     String response; // This will be the program's feedback to the user about the answer. 

     if (state == FIRST_TRY) { 
      if (userAnswer == question.getAnswer()) { 
       endTime = System.currentTimeMillis()/1000 - startTime; 
       response = "Good Job! That's correct. " 
         + "Time it took to answer Question: " + endTime + "sec(s)"; 
       correctFirstTry++; 
      } 
      else { // Keep the same question, but change state to SECOND_TRY. 
       response = "Sorry, that's not correct. Try again:"; 
       state = SECOND_TRY; 
      } 
     } 
     else { // state is SECOND_TRY 
      if (userAnswer == question.getAnswer()) { 
       endTime = System.currentTimeMillis()/1000 - startTime; 
       response = "Correct on the second try!" 
         + "Time it took to answer Question: " + endTime + "sec(s)"; 
       correctSecondTry++; 
      } 
      else { 
       endTime = System.currentTimeMillis()/1000 - startTime; 
       response = "Sorry, the correct answer is " + question.getAnswer() 
       + "Time it took to answer Question: " + endTime + "sec(s)"; 
       incorrect++; 
      } 
     } 

     display.setText(response); 
     answerInput.selectAll();  // Highlights the contents of the input box. 
     answerInput.requestFocus(); // Moves input focus to input box, so user doesn't have to click it. 

    } // end buttonPressed() 
    private void errorMessage(String message) { 
     JOptionPane.showMessageDialog(this, message, "Error in Input", JOptionPane.ERROR_MESSAGE); 
    } 
    public static void main(String[] args) { 
     JFrame window = new JFrame("Problems"); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     for(int i = 0; i < 8; i++){ 
      window.setContentPane(new Try()); 
      window.pack(); 
      window.setVisible(true); 
     } 
    } 
} 
+0

Stack Overflowは、「あなたは自分のコードを修正できますか」、「いくつかのコードの質問を書いてください」の場所ではありません。よい質問をすることについての助けのためにこのリンクを見てください:http://stackoverflow.com/help/mcve – 3ocene

答えて

0

は、基本的には、あなたのTryJPanel秒のすべてが含まれていますBoxLayoutなどのレイアウトマネージャを持つJPanelを分離するためにwindowのコンテンツペインを設定します。ループ内の新しいJPanelに各Tryインスタンスを追加します。次のようなもの:

+0

ああ、大丈夫です。どうもありがとう! – Trey

0

カードレイアウトを使用して、一度に1つのパネルを表示します。

関連する問題