java - 每行之前的 JRadioButtons

标签 java swing joptionpane jradiobutton

我有一个程序,它从文本文件中读取具有多项选择答案的问题,然后在 JOptionPane 中随机显示其中的一组(在新 Pane 中的每个问题)。在我的文本文件中,问题和 4 个答案选项都在一行中,然后我将它们分成新行。现在我想尝试在每个答案之前添加 JRadioButtons。有没有人可以帮助我。预先非常感谢您。这是我的代码:

Random random = new Random();
        for (int i = 0; i < newRanQues; i++) {

            int randIndex = random.nextInt(32) + 0;
            String randomQuestion = questions.get(randIndex);
            randomQuestions.add(randomQuestion);
            String different = randomQuestion.replaceAll(";", "\n");
            {
                JOptionPane.showMessageDialog(null, different, "Question", JOptionPane.INFORMATION_MESSAGE);

                JRadioButton answerA = new JRadioButton("A) " + answer[0]);
                JRadioButton answerB = new JRadioButton("B) " + answer[1]);
                JRadioButton answerC = new JRadioButton("C) " + answer[2]);
                JRadioButton answerD = new JRadioButton("D) " + answer[3]);

                ButtonGroup group = new ButtonGroup();
                group.add(optionA);
                group.add(optionB);
                group.add(optionC);
                group.add(optionD);

                return;
            }

最佳答案

这应该可以做到:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class RadioButtonExample {

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

    private static void init() {
        // create the jframe
        JFrame jframe = new JFrame("Question");
        // create the answers
        String[] answer = { "red", "green", "yellow", "blue, no, red, no... arrrrrg" };
        // create the radio buttons
        JRadioButton answerA = new JRadioButton("A) " + answer[0]);
        JRadioButton answerB = new JRadioButton("B) " + answer[1]);
        JRadioButton answerC = new JRadioButton("C) " + answer[2]);
        JRadioButton answerD = new JRadioButton("D) " + answer[3]);
        // create the button group
        ButtonGroup group = new ButtonGroup();
        group.add(answerA);
        group.add(answerB);
        group.add(answerC);
        group.add(answerD);
        // add the question to the jframe
        jframe.add(new JLabel("What is your favorite colour?"), BorderLayout.NORTH);
        // create gridbag layout and constraints
        GridBagLayout gbl = new GridBagLayout();
        // create the panel using the gbl
        JPanel pan = new JPanel(gbl);
        // create the constraints
        GridBagConstraints cons = new GridBagConstraints();
        cons.gridwidth = 1;
        cons.fill = GridBagConstraints.HORIZONTAL;
        // answer 1
        cons.gridx = 0;
        cons.gridy = 1;
        pan.add(answerA, cons);
        // answer 1
        cons.gridx = 0;
        cons.gridy = 2;
        pan.add(answerB, cons);
        // answer 1
        cons.gridx = 0;
        cons.gridy = 3;
        pan.add(answerC, cons);
        // answer 1
        cons.gridx = 0;
        cons.gridy = 4;
        pan.add(answerD, cons);
        // add the panel to the jframe
        jframe.add(pan, BorderLayout.CENTER);
        // show the jframe
        jframe.setSize(400, 400);
        jframe.setVisible(true);
    }
}

关于java - 每行之前的 JRadioButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30842489/

相关文章:

java - 如何从 Android 中的 Google Fit 服务检索健康数据

java - 从行边距中选择行 - mouseDragged 事件跟不上选择处理

Java system.out.print 打印 JOptionPane.ShowMessageDialog

java - YES_NO_OPTION JOptionPane 继续 'yes' 上的 java 代码

java - 如何使用 JOption.pane 在我的程序中输入 If 语句?

java - Java getter 和 setter 只是方法吗?

java - Oracle 时间戳值未转换为 java 日期时间

java - 已解析字段/信息 - 这意味着什么?

java - 如何打印整页Jtable

Java 堆栈到 GUI