2017-04-18 73 views
0

我有2个JComboBoxes填充了相同的条目(来自ENUM列表) 我有选择的项目更改时的行动事件,但我们有要求,我无法弄清楚。交换JComboBox选择

该代码正在转换货币...如果Box1 = USD和Box2 =欧元,然后我将Box1更改为= ERUO,则需要Box2 = USD。以下是我的动作监听器

 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); 
} 

}

+0

如果可能的话,请张贴整个班级,以便更方便地测试行为,而无需自己构建整个JFrame。 – DiabolicWords

+0

我继续前进,剥离出JFrame信息,仅仅是为了两个下拉菜单。 – Wes

回答

1

感谢您详细的职位。这是您的问题的解决方案。我对每个应用的三元运算符发表评论,以便清楚说明代码在这里的作用。

你需要做的是找出你目前在点击的组合框中设置的货币。然后在另一个框中设置相反的货币。我用三元运算符解决了这个问题。

// 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 

      ); 
     } 
    }); 

您现在可以添加触发货币计算等的代码的其余部分。

希望这会有所帮助。

+0

谢谢。这是做到了。 – Wes

+0

我的荣幸。 :) – DiabolicWords