说我想向JComboBox
添加一个JPanel
(或更一般的JRadioButton
,或许?),最简单的方法是什么?
Pseudo-wise,一个单选按钮组,其中一个包含多个选项,如下所示:
天气
O缔约方
O {meta,pseudo} -science
O动物
其中{}将是下拉列表。这里的诀窍是,如果单击下拉列表或标签' - science',单选按钮将被激活并显示UI边框和所有那些花哨的东西。
谢谢:)
答案 0 :(得分:3)
我讨厌这样的答案,但在这种情况下我觉得这是最好的......
这看起来像是非标准的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);
}
}
答案 1 :(得分:0)
我喜欢贾斯汀的回答,但另一个替代建议是:
将所有选项放在一个JComboBox中。
如果您真的想要从您的问题中选择路线,那么这是可能的。实现这一目标的最佳方法是: