gpt4 book ai didi

java - 如何在一系列单选按钮的顶部添加一个简单的字符串?

转载 作者:行者123 更新时间:2023-11-30 05:57:54 26 4
gpt4 key购买 nike

我正在尝试使用 Java 中的 GUI,并遇到了在一系列单选按钮上添加单个字符串的问题。最初,我试图模拟披萨订单。例如,我希望我的应用程序看起来像这样:

尺寸

.大披萨 - 10$
.超大披萨 - 15$
.小 - 7$

(假设项目符号点是实际的单选按钮)

这是我迄今为止所取得的成就:

class  SizePanel extends JPanel{

public SizePanel() {
setLayout(new GridLayout(3,1));

JLabel label1 = new JLabel();
label1.setText("Size");

// Create Radio buttons
JRadioButton large = new JRadioButton("Large - $11.99 + $1.50 / reg topping + $2.00 / prem topping", true);
JRadioButton xlarge = new JRadioButton("X-Large - $13.99 + $1.50 / reg topping + $2.00 / prem topping");
JRadioButton small = new JRadioButton("Small - $7.99 + $1.00 / reg topping + $1.50 / prem topping");

// Group the radio buttons
ButtonGroup sz = new ButtonGroup();
sz.add(large);
sz.add(xlarge);
sz.add(small);
add(label1);
add(large);
add(xlarge);
add(small);
}
}

但是,当我运行它时,输出并不是我所期望的。我得到这样的东西:

enter image description here

如何才能使单选按钮对齐并且单词(“大小”)位于单选按钮的顶部?

最佳答案

GridLayout 中指定的行数太小。 3 行 4 个组件(在一列中)没有意义。
这会导致布局不符合预期。使用

setLayout(new GridLayout(4,1));

或者更好,如卡洛斯·霍伯格 commented :

setLayout(new GridLayout(0,1)); //one column any number of rows

关于java - 如何在一系列单选按钮的顶部添加一个简单的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52846994/

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