将单选按钮连接到组合框

问题描述 投票:0回答:1

我正在编写一个关于 Java Swing 中的转换(质量、测量或体积)的程序。我想知道何时单击质量、测量值或体积中的任何一个 当我点击时,选项(g,kg,t)(cm,m,km)(cl,dl,l)发生变化

JRadioButton r1 = new JRadioButton("masse");
r1.setBounds(34, 43, 109, 23);
f.add(r1);

JRadioButton r2 = new JRadioButton("mesure");
r2.setBounds(34, 69, 109, 23);
f.add(r2);

JRadioButton r3 = new JRadioButton("volume");
r3.setBounds(34, 94, 109, 23);
f.add(r3);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2);bg.add(r3);
f.setLayout(null); 
final JComboBox list2 = new JComboBox();
list2.setModel(new DefaultComboBoxModel(new String[] {"choisir ", "kg", "g", "t"}));
list2.setBounds(193, 172, 64, 22);
f.add(list2);
final JComboBox list2 = new JComboBox();
list2.setModel(new DefaultComboBoxModel(new String[] {"choisir ", "cm", "m", "km"}));
list2.setBounds(193, 172, 64, 22);
f.add(list2);
final JComboBox list2 = new JComboBox();
list2.setModel(new DefaultComboBoxModel(new String[] {"choisir ", "cl", "dl", "l"}));
list2.setBounds(193, 172, 64, 22);
f.add(list2);

enter image description here

java swing
1个回答
0
投票

要解决此问题,您需要执行以下操作:

  • 在程序中声明一个 DefaultComboBoxModel 字段。这将允许您更改 JComboBox 在程序运行时使用的模型
  • 创建一个使用相同模型的 JComboBox
  • 创建 JRadioButtons,将它们分配到同一个 ButtonGroup 并给它们一个 ActionListener
    • 首先清除组合框模型
    • 然后将适当的字符串分配到同一模型中

例如:

import java.util.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;

public class RadioCombo {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("RadioCombo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new RadioComboPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

class RadioComboPanel extends JPanel {
    private static final Map <String, List<String>> SELECTIONS = Map.of(
        "A", List.of("A1", "A2", "A3"),
        "B", List.of("B1", "B2", "B3"),
        "C", List.of("C1", "C2", "C3")
    );
    private DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
    private JComboBox<String> combo = new JComboBox<>(model);

    public RadioComboPanel() {
        // create a sorted map, if desired -- this is not 100% necessary
        Map<String, List<String>> sortedSelections = new TreeMap<>(SELECTIONS);
        JPanel comboPanel = new JPanel();
        comboPanel.add(combo);
        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
        ButtonGroup group = new ButtonGroup();
        for (String key : sortedSelections.keySet()) {
            JRadioButton button = new JRadioButton(key);
            button.setActionCommand(key);
            button.addActionListener(e -> {
                model.removeAllElements();
                for (String value : SELECTIONS.get(key)) {
                    model.addElement(value);
                }
            });
            group.add(button);
            radioPanel.add(button);
        }

        setLayout(new BorderLayout());
        add(combo, BorderLayout.PAGE_END);
        add(radioPanel, BorderLayout.CENTER);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.