Trey Trey - 1 year ago 90
Java Question

How to add multiple Panels to JFrame?

I want to add 8 math problems to a JFrame. Everything is a work in
progress, but in case it is helpful here is my
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;


Here is my main class. This is of course where the problem lies. I'm
creating 8 math problems (all the same problems as of right now), and
I'm wanting to get all 8 problems onto the same JFrame. I thought
using a loop would work since I create the JFrame and then loop
through the
class, but that doesn't work. So how would I do

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));
JPanel bottom = new JPanel();
bottom.add(new JLabel("Enter Answer Here: "));
answerInput = new JTextField(4);
button = new JButton("Show Question");
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent evt) {
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());
state = FIRST_TRY;

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

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

// 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)";
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)";
else {
endTime = System.currentTimeMillis()/1000 - startTime;
response = "Sorry, the correct answer is " + question.getAnswer()
+ "Time it took to answer Question: " + endTime + "sec(s)";

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");
for(int i = 0; i < 8; i++){
window.setContentPane(new Try());

Answer Source

Basically you want to set the content pane of window to separate JPanel that has a layout manager such as BoxLayout that will contain all of your Try JPanels. Then add each Try instance to the new JPanel in your loop. Something like:

JFrame window = new JFrame("Problems");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout());

for(int i = 0; i < 8; i++) {
   panel.add(new Try());

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download