2017-04-18 60 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