使用GUI的货币转换器 - Java

时间:2016-05-05 18:13:52

标签: java user-interface hashmap

我需要使用简单的GUI界面创建货币转换器应用程序。 我创建了一个包含所有货币和值的地图。但我无法弄清楚如何从一种货币转换为另一种货币。我用美元作为这个公式的基线:

xx.xx巴西雷亚尔=((1 / 3.61)* xx.xx)美元=(((1 / 3.61)* xx.xx)*(66.42))印度卢比

在此代码的底部是部分,我无法在此课程中进一步学习。

到目前为止,这是我的代码。

public class CurrencyConverter
    extends JPanel
    implements ActionListener
 {



private JLabel     fromLabel;
private JLabel     toLabel;
private JTextField fromField;
private JTextField toField;
private JComboBox  fromCombo;
private JComboBox  toCombo;

private JPanel     labelPanel; 
private JPanel     fromPanel;
private JPanel     toPanel;   


public CurrencyConverter()
{



    fromLabel = new JLabel("1 US Dollar equals");
    fromLabel.setName("fromLabel");
    fromLabel.setFont(fromLabel.getFont().deriveFont(8.0f));
    fromLabel.setForeground(Color.gray);

    toLabel = new JLabel("0.88 Euro");
    toLabel.setName("toLabel");
    toLabel.setFont(toLabel.getFont().deriveFont(20.0f));


    fromField = new JTextField("1", 10);
    fromField.setName("fromField");
    fromField.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            convert();

        }

    });

    toField = new JTextField("0.88", 10);
    toField.setName("toField");

    String[] fromComboItems =
            { "British Pound", "Brazilian Real", "Chinese Yuan", "Euro",
                    "Indian Rupee", "Japanese Yen", "Russian Ruble",
                    "US Dollar" };
    fromCombo = new JComboBox<>(fromComboItems);
    fromCombo.setName("fromCombo");

    String[] toComboItems =
            { "British Pound", "Brazilian Real", "Chinese Yuan", "Euro",
                    "Indian Rupee", "Japanese Yen", "Russian Ruble",
                    "US Dollar" };
    toCombo = new JComboBox<>(toComboItems);
    toCombo.setName("toCombo");

    labelPanel = new JPanel();
    fromPanel = new JPanel();
    toPanel = new JPanel();


    BoxLayout lableBoxLayout =
            new BoxLayout(labelPanel, BoxLayout.PAGE_AXIS);
    labelPanel.setLayout(lableBoxLayout);
    labelPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    labelPanel.setAlignmentX(LEFT_ALIGNMENT);
    labelPanel.add(fromLabel);
    labelPanel.add(toLabel);

    fromPanel.setAlignmentX(LEFT_ALIGNMENT);
    fromPanel.add(fromField);
    fromPanel.add(fromCombo);

    toPanel.setAlignmentX(LEFT_ALIGNMENT);
    toPanel.add(toField);
    toPanel.add(toCombo);


    BoxLayout boxLayout = new BoxLayout(this, BoxLayout.PAGE_AXIS);
    this.setLayout(boxLayout);
    this.add(labelPanel);
    this.add(fromPanel);
    this.add(toPanel);
}


private void fromFieldActionPerformed(ActionEvent e)
{
    convert();
}



private Map<String, Double> Currency = new HashMap<>();
{
 Currency.put("US Dollar", 1.00);
 Currency.put("British Pound", 0.70 );
 Currency.put("Brazilian Real", 3.61);
 Currency.put("Chines Yuan", 6.48);
 Currency.put("Euro", 0.88);
 Currency.put("Indian Rupee", 66.42);
 Currency.put("Japanese Yen", 108.83);
 Currency.put("Russian Ruble", 66.12);


 }

  private void convert() {
   double fromAmount;
   double toAmount;
   double fromAmoun = Currency.get(fromCombo.getSelectedItem().toString());


   try {
     fromAmount = Double.parseDouble(fromField.getText());
 } catch (NumberFormatException e) {
     return; // do nothing
 }
 toAmount =  // ****not sure what to do here..****
 String toFieldText = String.format("%.2f", toAmount);
toField.setText(toFieldText);
 }

@Override
public void actionPerformed(ActionEvent arg0)
{
    // TODO Auto-generated method stub

}
}

0 个答案:

没有答案