我想在JFrame中添加8个数学问题。一切都是工作 进度,但如果它有用,我的
class QuizQuestion
。
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 :(得分:0)
基本上,您希望将window
的内容窗格设置为单独JPanel
,其中包含BoxLayout等布局管理器,其中包含您的所有Try
{{1} }}秒。然后将每个JPanel
实例添加到循环中的新Try
。类似的东西:
JPanel
答案 1 :(得分:0)
使用卡片布局并一次显示一个面板。