Java - 根据单选按钮

时间:2017-08-26 18:53:17

标签: java swing combobox radio-button

我正在尝试出租车预订申请。现在,如果我(例如)想通过移动应用程序安排,那么我想启用带有驱动程序名称的组合框,如果有人通过电话调度,那么我想启用带有调度程序名称的组合框。怎么样?我在initActions()尝试了一些东西,但显然它不起作用......

public class OrderWindow extends JFrame {

    private JLabel lblCustomerName;
    private JTextField txtCustomerName;
    private JLabel lblDateOrder;
    private JPanel pnlDateOrder; 
    private JLabel lblDepartureAdress;
    private JTextField txtADepartureAdress`
    private JComboBox cbDriver;
    private JRadioButton rbMobileApp;
    private JRadioButton rbPhoneCall;
    private ButtonGroup bgOrder;

    public OrderWindow(){
        setTitle("Scheduling");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        initGUI();
        initActions();
    }



    private void initActions() {
        rbMobileApp.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource()==rbMobileApp) {
                    setEnabled(rbMobilnaAplikacija.isSelected());
                } 
            }
        });
    }

    private void initGUI() {
        MigLayout mig = new MigLayout("wrap 2", "[][]", "[]10[][]10[]");
        setLayout(mig);
        lblCustomerName = new JLabel("Name and Lastname");
        txtCustomerName = new JTextField(20);

        lblDepartureAdress = new JLabel("Adress");
        txtDepartureAdress = new JTextField(20);

        rbMobileApp = new JRadioButton("Application");
        rbPhoneCall = new JRadioButton("Call");
        bgPorudzbina = new ButtonGroup();
        add(lblCustomerName);
        add(txtCustomerName);
        add(lblDepartureAdress);
        add(txtDepartureAdress);
        add(rbMobileApp);
        add(rbPhoneCall);
        bgOrder = new ButtonGroup();
        bgOrder.add(rbMobileApp);
        bgOrder.add(rbPhoneCall);   
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

如果你只有2个JRadioButtons和2个JComboBox,那么解决方案很简单:给JRadioButtons一个ItemListener来检查是否选中了收音机,如果是,选择相应的JComboBox。

如,

radioBtn.addItemListener(evt -> {
    combo.setEnabled(evt.getStateChange() == ItemEvent.SELECTED);
});

如果你有一堆JRadioButton / JComboBox组合,那么你需要一种更强大的方法来连接这两者,这可以通过使用Map(如HashMap)或将两个对象放入他们自己的类来实现,例如:

enter image description here

import java.awt.event.ItemEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;

public class RadioCombo<T> {
    private JRadioButton radioBtn;
    private JComboBox<T> combo;

    public RadioCombo(String text, DefaultComboBoxModel<T> model) {
        radioBtn = new JRadioButton(text);
        combo = new JComboBox<>(model);
        combo.setEnabled(false);
        radioBtn.addItemListener(evt -> {
            combo.setEnabled(evt.getStateChange() == ItemEvent.SELECTED);
        });
    }

    public JRadioButton getRadioBtn() {
        return radioBtn;
    }

    public JComboBox<T> getCombo() {
        return combo;
    }
}

然后你可以像这样使用它:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class TestRadioCombo extends JPanel {
    private static final String[] DATA = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
    private static final String[] INNER_DATA = {"One", "Two", "Three", "Four", "Five"};
    private static final int GAP = 3;
    public TestRadioCombo() {
        ButtonGroup buttonGroup = new ButtonGroup();

        setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        setLayout(new GridBagLayout());
        for (String datum : DATA) {

            DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
            for (String innerDatum : INNER_DATA) {
                String item = datum + " - " + innerDatum;
                model.addElement(item);
            }
            RadioCombo<String> radioCombo = new RadioCombo<>(datum, model);
            buttonGroup.add(radioCombo.getRadioBtn());
            addRadioCombo(radioCombo);
        }
    }

    private void addRadioCombo(RadioCombo<String> radioCombo) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = GridBagConstraints.RELATIVE;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(GAP, GAP, GAP, 2 * GAP);
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        add(radioCombo.getRadioBtn(), gbc);

        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(GAP, GAP, GAP, GAP);
        add(radioCombo.getCombo(), gbc);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new TestRadioCombo());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

另一个选择是拥有一堆JRadioButtons并且只有一个 JComboBox,然后在你的单选按钮项侦听器中,交换JComboBox的 模型 取决于选择了哪个JRadioButton。