2017-08-26 58 views
2

我正在尝试使出租车预订应用程序。现在,如果我(例如)想通过移动应用程序进行安排,那么我想要启用带有驱动程序名称的组合框,并且如果有人通过电话进行安排,那么我想启用具有调度程序名称的组合框。怎么样?我tryied东西initActions(),但是,很明显它不工作...Java - 根据单选按钮启用组合框

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

如果只有2个JRadioButton和2个JComboBoxes,那么解决方法很简单:给JradioButton将一个ItemListener支票如果选择了收音机,并且如果是,请选择相应的JComboBox。

例如,

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

如果你有一大堆的一个JRadioButton/JComboBox的组合,那么你需要一个更强大的方式来连接两个,而且可以通过使用地图,例如一个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()); 
    } 
} 

另一种选择是有一堆JradioButton将和只有一个 JComboBox中,然后在你的单选按钮项侦听器,交换JcomboBox的模型,这取决于一个JRadioButton选择。