gpt4 book ai didi

java - JRadioButton 中的 JComboBox

转载 作者:行者123 更新时间:2023-12-04 06:36:29 25 4
gpt4 key购买 nike

假设我想添加一个 JComboBox (或者更一般的 JPanel ,也许?)到 JRadioButton ,最简单的方法是什么?

伪明智的,其中一个包括多个选项的单选按钮组看起来像:

O天气
O 缔约方
O {元,伪}-科学
O 动物

其中 {} 将是一个下拉列表。这里的技巧是,如果单击下拉列表或标签“-science”,单选按钮将被激活并显示 UI 边框和所有这些花哨的东西。

谢谢 :)

最佳答案

我讨厌给出这样的答案,但在这种情况下,我觉得最好......

这似乎是一个非常不标准的 UI 组件。如果您只是这样做,那将是更好的用户体验:

O The weather
O Parties
O meta-science
O pseudo-science
O Animals

用户不会熟悉您提议的组件类型,并且与列表中的其他选项非常不一致。我强烈建议使用更标准的约定。

根据我更好的判断,我向您提供 ComboBoxRadioButton :
它不完整,我也不建议使用它,但它看起来像你想要的。
import java.awt.FlowLayout;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;

public class ComboBoxRadioButton extends JRadioButton {

private JLabel beforeText, afterText;
private JComboBox comboBox;

public ComboBoxRadioButton(String beforeTxt, JComboBox comboBox,
String afterText) {
this.comboBox = comboBox;
this.beforeText = new JLabel(" " + beforeTxt);
this.afterText = new JLabel(afterText);
comboBox.setSelectedIndex(0);
setLayout(new FlowLayout());
setModel(new JToggleButton.ToggleButtonModel());
add(this.beforeText);
add(this.comboBox);
add(this.afterText);
}

public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPane = new JPanel();
ButtonGroup group = new ButtonGroup();
AbstractButton b2 = new JRadioButton("Java Swing");
AbstractButton b3 = new ComboBoxRadioButton(
"It's gonna be a", new JComboBox(new String[] { "good", "bad",
"rainy" }), "day!");
AbstractButton b4 = new JRadioButton("After the combo");
group.add(b2);
group.add(b3);
group.add(b4);
mainPane.add(b2);
mainPane.add(b3);
mainPane.add(b4);
f.add(mainPane);
f.pack();
f.setVisible(true);
}
}

关于java - JRadioButton 中的 JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4818412/

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