我有 2 个 JComboBox,填充了相同的条目(来自 ENUM 列表) 我有所选项目更改时的操作事件,但我们有我不太明白的要求。
代码正在转换货币...如果 Box1 = USD 且 Box2 = EURO,然后我将 Box1 更改为 = ERUO,我需要 Box2 更改为 = USD。下面是我的actionListeners
fromCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String finalAmt = convertCurr(fromField.getText(),
fromCombo.getSelectedItem().toString(),
toCombo.getSelectedItem().toString());
//Check for Errors
try {
Double.parseDouble(finalAmt);
//CHANGE LABELS
toLabel.setText(finalAmt + " " +
toCombo.getSelectedItem().toString());
toField.setText(String.valueOf(finalAmt));
} catch (NumberFormatException nfe) {
fromLabel.setText(finalAmt);
toLabel.setText(finalAmt);
toField.setText(finalAmt);
} finally {
fromLabel.setText(fromField.getText() + " " +
fromCombo.getSelectedItem().toString() + " equals");
}
}
});
toCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String finalAmt = convertCurr(fromField.getText(),
fromCombo.getSelectedItem().toString(),
toCombo.getSelectedItem().toString());
//Check for Errors
try {
Double.parseDouble(finalAmt);
//CHANGE LABELS
toLabel.setText(finalAmt + " " +
toCombo.getSelectedItem().toString());
toField.setText(String.valueOf(finalAmt));
} catch (NumberFormatException nfe) {
fromLabel.setText(finalAmt);
toLabel.setText(finalAmt);
toField.setText(finalAmt);
} finally {
fromLabel.setText(fromField.getText() + " " +
fromCombo.getSelectedItem().toString() + " equals");
}
}
});
fromField.postActionEvent();
谁能帮我解决这个问题吗?需要更多信息吗?
编辑:这是一个精简后的示例代码库。
public class tDropDowns extends JPanel implements ActionListener {
private final JComboBox<CurrencyConstant> fromCombo;
private final JComboBox<CurrencyConstant> toCombo;
public tDropDowns() {
fromCombo = new JComboBox<>(CurrencyConstant.values());
fromCombo.setName("fromCombo");
toCombo = new JComboBox<>(CurrencyConstant.values());
toCombo.setName("toCombo");
// TODO: Layout code goes here...
JPanel entryFields = new JPanel();
entryFields.setLayout(new GridBagLayout());
//entryFields.setBorder(new EmptyBorder(10, 10, 10, 10));
entryFields.setAlignmentX(Component.LEFT_ALIGNMENT);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 10, 5, 10);
gbc.gridx = 0;
gbc.gridy = 0;
entryFields.add(fromCombo, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
entryFields.add(toCombo, gbc);
this.add(entryFields);
// Set initial values:
fromCombo.setSelectedItem(CurrencyConstant.USD);
toCombo.setSelectedItem(CurrencyConstant.EUR);
fromCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
toCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
@Override
public void actionPerformed(ActionEvent arg0)
{
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Currency Converter Dropdowns");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(300, 150));
JComponent newContentPane = new tDropDowns();
newContentPane.setLayout(new BoxLayout(newContentPane,
BoxLayout.PAGE_AXIS));
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(tDropDowns::createAndShowGUI);
}
}
最佳答案
感谢您的详细帖子。这是您问题的解决方案。我对每个应用的三元运算符添加了注释,以便清楚地说明代码在这里的作用。
您要做的就是找出您当前在单击的组合框中设置的货币是什么。然后在另一个框中设置相反的货币。我用三元运算符解决了这个问题。
// Set initial values:
fromCombo.setSelectedItem(CurrencyConstant.USD);
toCombo.setSelectedItem(CurrencyConstant.EUR);
fromCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
toCombo.setSelectedItem(
// Is EUR in fromCombo selected? Then set USD in toCombo. Else set EUR in toCombo.
(fromCombo.getSelectedItem() == CurrencyConstant.EUR) ? CurrencyConstant.USD : CurrencyConstant.EUR
);
}
});
toCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fromCombo.setSelectedItem(
// Is EUR in toCombo selected? Then set USD in fromCombo. Else set EUR in fromCombo.
(toCombo.getSelectedItem() == CurrencyConstant.EUR) ? CurrencyConstant.USD : CurrencyConstant.EUR
);
}
});
您现在可以添加触发货币计算等的其余代码。
希望这有帮助。
关于java - 交换 JComboBox 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43480821/