gpt4 book ai didi

Java - JRadioButton选择出现面板

转载 作者:行者123 更新时间:2023-12-01 20:21:17 24 4
gpt4 key购买 nike

我需要确保当我选择特定的单选按钮时,会出现一个包含三个 JSpinner 和三个 JLabel 的 JPanel。

我写了这段代码:

JRadioButton custom = new JRadioButton("Custom");
SpinnerModel spinnerModel = new SpinnerNumberModel(2, 0, 80, 1);
JSpinner height = new JSpinner(spinnerModel);
SpinnerModel spinnerModel2 = new SpinnerNumberModel(2, 0, 80, 1);
JSpinner width = new JSpinner(spinnerModel2);
SpinnerModel spinnerModelMine = new SpinnerNumberModel(1, 0, 99, 1);
JSpinner mines = new JSpinner(spinnerModelMine);

当我选择自定义 JRadioButton 时,应该出现此 JPanel:

GroupLayout panelEstGroupLayout = new GroupLayout(panelEst);

panelEstGroupLayout.setHorizontalGroup(
panelEstGroupLayout.createParallelGroup(Alignment.LEADING)
.addGroup
(
panelEstGroupLayout.createSequentialGroup()
.addGap(5)
.addGroup
(
panelEstGroupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(startGame)
.addComponent(boxTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
)
)
.addGroup
(
panelEstGroupLayout.createSequentialGroup()
.addContainerGap()
.addGroup
(
panelEstGroupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup
(
Alignment.LEADING, panelEstGroupLayout.createSequentialGroup()
.addGap(6)
.addComponent(heightLabel, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(widthLabel, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(minesLabel, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
)
.addGroup
(
Alignment.LEADING, panelEstGroupLayout.createSequentialGroup()
.addComponent(height, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(width, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mines, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
)
)
)
);
panelEstGroupLayout.setVerticalGroup(
panelEstGroupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(panelEstGroupLayout.createSequentialGroup()
.addGap(5)
.addComponent(boxTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGroup(panelEstGroupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(panelEstGroupLayout.createSequentialGroup()
.addGap(74)
.addComponent(startGame))
.addGroup(Alignment.TRAILING, panelEstGroupLayout.createSequentialGroup()
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(panelEstGroupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(heightLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(widthLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(minesLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(panelEstGroupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(height)
.addComponent(width, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(mines, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(43)))
.addGap(0))
);

我将此代码插入 if 条件为:if(custom.isSelected()) 但它不起作用。为什么?

最佳答案

您应该为单选按钮注册一个操作监听器,当它发生更改时,您可以根据单选按钮状态隐藏/取消隐藏或禁用/启用面板。

至于面板创建时,您可以选择立即创建它,但在需要时将其隐藏/禁用。或者,您只能在操作进入后创建它。就我个人而言,我更喜欢在开始时创建所有元素,而不是在整个代码中分发 GUI 元素创建。

关于Java - JRadioButton选择出现面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44654770/

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