gpt4 book ai didi

java - 如何组织 JFrame 上的单选按钮?

转载 作者:行者123 更新时间:2023-12-01 13:00:38 24 4
gpt4 key购买 nike

在下面的代码中,我无法将界面组织为看起来像所附照片。我尝试过网格和流布局,但无法解决问题。

The GUI should look like this

        package app;
import javax.swing.*;
import java.awt.*;

public class app{
JFrame f1;
JButton buttton_1,buttton_2;
JLabel label_1,label_2;
JRadioButton radio_1,radio_2,radio_3,radio_4,radio_5,radio_6,radio_7;

app(){

JFrame f1 = new JFrame ("MathTest - Main Menu");
f1.setVisible(true);
f1.setSize(500,500);
f1.setLayout(new GridLayout(0,1));
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


label_1 =new JLabel("select test type ");
radio_1=new JRadioButton("addition ");
radio_2=new JRadioButton("subtraction");
radio_3=new JRadioButton("mulitipcation");
radio_4=new JRadioButton("division");

label_2 =new JLabel("select a diffculty level");
radio_5=new JRadioButton("easy ");
radio_6=new JRadioButton("moderate");
radio_7=new JRadioButton("hard");

buttton_1=new JButton("start test");
buttton_2=new JButton("exit");

f1.add(label_1);
f1.add(radio_1);
f1.add(radio_2);
f1.add(radio_3);
f1.add(radio_4);

f1.add(label_2);
f1.add(radio_5);
f1.add(radio_6);
f1.add(radio_7);
f1.add(buttton_1);
f1.add(buttton_2);

}

public static void main(String[] args)
{
app xyz =new app();
}
}

最佳答案

无需为您做所有事情,您可以通过以下方式实现其中一个单选按钮面板:

JPanel typePanel = new JPanel(new GridLayout(0, 2));
typePanel.setBorder(BorderFactory.createLineBorder(Color.black, 1));
typePanel.add(new JLabel("Select a test type"));
typePanel.add(new JRadioButton("Addition"));
typePanel.add(new JLabel(""));
typePanel.add(new JRadioButton("Substraction"));
typePanel.add(new JLabel(""));
typePanel.add(new JRadioButton("Multiplication"));
typePanel.add(new JLabel(""));
typePanel.add(new JRadioButton("Division"));

对于底部的按钮,请记住可以设置流布局以将右侧的内容与构造函数对齐:

new FlowLayout(FlowLayout.RIGHT)

关于java - 如何组织 JFrame 上的单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23539653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com