0

As exercise, I am trying to make an application to make a quiz but I can't figure out how to make a layout similar to this:

 JOptionPane.showConfirmDialog(panel,"How to make this layout?","QUIZ FRAME",JOptionPane.YES_NO_CANCEL_OPTION);

In particular I can't find a way to insert the question in the first line and all the remainig buttons to a different line (or multiple lines if long answers). I tried with every possible layout manager but I either get one object per line (GridLayout) or everything in one line like in FlowLayout.

I figure it out how to use ActionEvent to slide to the next panel (question) if I click on a button (answer) and everything else but not this simple layout I am looking for. Any advice?

EDIT: my intention was to make the same layout but without using JOptionPane. I managed to do something similar from scratch with multiple panels and nested layouts. I am just a beginner so I am sure there must be some other better way to do it.

public static void main(String[] args)  {

    JFrame frame = new JFrame("QUIZ FRAME");
    JPanel panel = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();

    JLabel question1 = new JLabel("QUESTION 1");
    JLabel questiontext = new JLabel("   TEXT FOR QUESTION NUMBER 1 ");
    JButton answer = new JButton("Answer");
    JButton answer2 = new JButton("Answer2");
    JButton answer3 = new JButton("Answer3");

    answer.setSize(50,50);
    answer2.setSize(50,50);
    answer3.setSize(50,50);

    BorderLayout framelayout = new BorderLayout();
    BorderLayout panellayout = new BorderLayout();
    BorderLayout panellayout2 = new BorderLayout();
    GridLayout panellayout3 = new GridLayout(1,3,30,30);

    frame.setLayout(framelayout);
    panel.setLayout(panellayout);
    panel2.setLayout(panellayout2);
    panel3.setLayout(panellayout3);
    panel.setBackground(Color.BLUE);
    panel2.setBackground(Color.RED);
    panel.setPreferredSize(new Dimension( 500,30));
    panel2.setPreferredSize(new Dimension(500,50));
    frame.add(panel, framelayout.NORTH);
    frame.add(panel2,framelayout.CENTER);
    frame.add(panel3,framelayout.SOUTH);

    question1.setHorizontalAlignment(0);
    question1.setVerticalAlignment(0);
    panel.add(question1);

    panel2.add(questiontext, panellayout2.WEST);
    panel3.add(answer);
    panel3.add(answer2);
    panel3.add(answer3);

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
  • 1
    Almost any complex Swing GUI requires "multiple panels and nested layouts". Perhaps this Oracle tutorial might help. https://docs.oracle.com/javase/tutorial/uiswing/index.html This Stack Overflow answer, although for a different GUI, goes through the steps to complete a complex GUI. https://stackoverflow.com/questions/62839159/how-would-i-make-a-gui-using-swing-that-picks-two-elements-from-a-list-and-prese/62866517#62866517 – Gilbert Le Blanc Jul 13 '20 at 12:13

2 Answers2

0

Try this answer: https://stackoverflow.com/a/8658721/13912132

String[] buttons = { "Java","C","Python","C++"};    
int returnValue = JOptionPane.showOptionDialog(null, "What's the best programming language", "Question",
        JOptionPane.WARNING_MESSAGE, 0, null, buttons, buttons[0]);
//Do something with returnValue
JCWasmx86
  • 3,473
  • 2
  • 11
  • 29
  • my intention was to make the same layout but without using JOptionPane. I managed to do something similar from scratch with multiple panels and nested layouts. I am just a beginner so I am sure there must be some other better way to do it. – Matteo Scalas Jul 13 '20 at 11:33
  • Oh, ok, sorry I misunderstood you. – JCWasmx86 Jul 13 '20 at 15:34
0

You could use a JFrame as your main window, to generate questions and manage answers.
Use a JOptionPane to display questions and return answers:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Main {

    private final JLabel answerLabel;
    private final JButton askButton;

    Main(){
        JFrame frame = new JFrame("QUIZ FRAME");
        JPanel askPane = new JPanel();
        askButton = new JButton("Ask me");
        askButton.addActionListener(e->ask());
        askPane.add(askButton);

        JPanel answerPane = new JPanel();
        answerPane.setPreferredSize(new Dimension(300,80));
        answerLabel = new JLabel();
        answerPane.add(answerLabel);

        frame.add(answerPane,BorderLayout.CENTER);
        frame.add(askPane,BorderLayout.SOUTH);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void ask() {
        answerLabel.setText("");
        String[] answers = {"Brazil", "Argentina", "Mexico"};
        String question = "Which countrey has the largest population ? ";
        int answer = new QuizDialog(answers, question, "Quiz").show();
        answerLabel.setText("You answered "+ answers[answer]);
    }

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

class QuizDialog {

    private final String[] answers;
    private final JComponent question;
    private final String title;

    public QuizDialog(String[] options, String questionText, String title) {
        answers = options;
        question = new JLabel(questionText);
        this.title = title;
    }

    public int show() {
        return JOptionPane.showOptionDialog(null, question, title,
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, answers, null);
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65