【问题标题】:JComboBox within a JRadioButtonJRadioButton 中的 JComboBox
【发布时间】:2011-01-27 15:39:21
【问题描述】:

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

在伪方面,其中一个包含多个选项的单选按钮组看起来像:

天气
派对
O {元,伪}-科学
哦动物

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

谢谢:)

【问题讨论】:

    标签: java swing jcombobox jradiobutton


    【解决方案1】:

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

    这似乎是一个非常不标准的 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);
        }
    }
    

    【讨论】:

    • 是的非标准 UI FTW,我为什么要遵守? :) 您的解决方案很简单,尽管有“很多”选项,并且假设其他单选按钮选项确实相关且相关,我认为这不是一个可行的解决方案。另外,我真的很想看到 Java 大师抛出一些漂亮的代码来炫耀:)
    • @Juhl,你要的,所以就在这里。它不完整,我也不建议使用它。查看我的编辑。
    【解决方案2】:

    我喜欢贾斯汀的回答,但还有另一个建议:

    将所有选项放在一个 JComboBox 中。

    如果你真的想从你的问题中走出来,这是可能的。实现这一目标的最佳方法是:

    • 创建一个 JPanel,其中 JRadioButton 在左侧,Combo 在中间,标签在右侧。
    • 添加鼠标侦听器以捕捉面板上的点击。
    • 调整边框、布局和可能的其他 UI 项,使其看起来更漂亮。

    【讨论】:

    • 是的,想过,但是组件的跨平台L&F呢?我想最困难的方法是像 JRadioButton 那样扩展 javax.swing.AbstractButtonjavax.swing.JToggleButton 然后设计自下而上的行为。
    • @Juhl,不确定你有多远,但如果你指定像 Nimbus 的外观和感觉,那么跨平台外观将比默认操作系统外观更接近。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 2017-03-13
    相关资源
    最近更新 更多