【问题标题】:Quiz Layout using Swing使用 Swing 的测验布局
【发布时间】:2020-11-02 11:24:19
【问题描述】:

作为练习,我正在尝试制作一个应用程序来进行测验,但我不知道如何制作类似于此的布局:

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

特别是我找不到将问题插入第一行并将所有剩余按钮插入另一行(如果答案很长,则为多行)的方法。我尝试了所有可能的布局管理器,但我要么每行获得一个对象(GridLayout),要么像 FlowLayout 一样在一行中获得所有内容。

如果我单击按钮(答案)和其他所有内容,但不是我正在寻找的这个简单布局,我会弄清楚如何使用 ActionEvent 滑动到下一个面板(问题)。有什么建议吗?

编辑: 我的意图是制作相同的布局,但不使用 JOptionPane。我设法用多个面板和嵌套布局从头开始做类似的事情。我只是一个初学者,所以我相信一定有其他更好的方法来做到这一点。

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);
}

【问题讨论】:

标签: java swing layout joptionpane


【解决方案1】:

试试这个答案: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

【讨论】:

  • 我的意图是制作相同的布局但不使用 JOptionPane。我设法用多个面板和嵌套布局从头开始做类似的事情。我只是一个初学者,所以我相信一定有其他更好的方法来做到这一点。
  • 哦,好吧,对不起,我误解了你。
【解决方案2】:

您可以使用JFrame 作为您的主窗口,以生成问题和管理答案。
使用JOptionPane 显示问题并返回答案:

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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 2012-01-29
    • 2019-04-04
    • 2018-05-03
    • 2014-12-20
    • 1970-01-01
    • 2010-10-07
    相关资源
    最近更新 更多