我正在尝试预订出租车。现在,例如,如果我想通过移动应用程序进行调度,那么我想启用带有驱动程序名称的组合框,如果有人通过电话进行调度,那么我想启用具有调度程序名称的组合框。怎么样?我尝试了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);
}
}
最佳答案
如果只有2个JRadioButtons和2个JComboBoxes,则解决方案很简单:给JRadioButtons一个ItemListener来检查是否选择了单选,如果选择,则选择相应的JComboBox。
例如。,
radioBtn.addItemListener(evt -> {
combo.setEnabled(evt.getStateChange() == ItemEvent.SELECTED);
});
如果您有一堆JRadioButton / JComboBox组合,那么您需要一种更可靠的方式来连接两者,这可以通过使用诸如HashMap之类的Map或将两个对象放入自己的类中来实现。就像是:
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,然后在单选按钮项侦听器中,根据选择的JRadioButton交换JComboBox的模型。
关于java - Java-根据单选按钮启用组合框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45898672/