简体   繁体   English

Java:将ArrayList与JComboBox和JTextField结合使用

[英]Java: Using ArrayList with a JComboBox and JTextField

I am currently trying to create a GUI which is fed information from a .txt file which contains exchange rates. 我目前正在尝试创建一个GUI,该GUI从包含汇率的.txt文件中获取信息。

The text file contains 3 different values which are separated with a ','. 文本文件包含3个不同的值,并用','分隔。

The values are: Currency Name, Exchange Rate, Currency Symbol. 值是:货币名称,汇率,货币符号。

I have the program working so that it stores these 3 values into 3 different ArrayLists 我正在运行程序,以便将这3个值存储到3个不同的ArrayLists中

The program also updates the JComboBox with the Currency Name. 该程序还将使用货币名称更新JComboBox。

However, I now need the program to select the correct Exchange Rate and Currency Symbol based on what Currency Name the user has chosen in the JComboBox. 但是,我现在需要该程序根据用户在JComboBox中选择的货币名称来选择正确的汇率和货币符号。

For example, if the user picks USD (arrayCurrency) and inputs 5 into the JTextField the program needs to chose the correct Exchange Rate (arrayRate). 例如,如果用户选择USD(arrayCurrency)并将5输入到JTextField中,则程序需要选择正确的汇率(arrayRate)。

However, I now need the program to select the correct Exchange Rate and Currency Symbol based on what Currency Name the user has chosen in the JComboBox. 但是,我现在需要该程序根据用户在JComboBox中选择的货币名称来选择正确的汇率和货币符号。

This isn't exactly a question... I'm assuming you're asking for the following: 这不完全是一个问题...我假设您要输入以下内容:

How do I get the value from a JComboBox? 如何从JComboBox获取值? -> yourJCBox.getSelectedItem(); -> yourJCBox.getSelectedItem();

Now that you have this String - you need to decide how to map the exchange rates to the dollar amounts. 现在有了该字符串-您需要确定如何将汇率映射到美元金额。 Whether it be 2d, 3d arrays, or an object of your devising: 无论是2d,3d阵列还是您设计的对象:

public class myCurrency() {
  String currnecy;
  String symbol;
  Double rate;

  public myCurrency(String c, String s, Double r) {
    // Constructor stuff
  }

  // Getters and setters
}

Now you have idea for everything you need to do to check if some currency selected should be displayed with some exchange rate and some symbol. 现在,您有了所有需要检查的想法,以检查是否应显示某些汇率和符号的货币。

You can manage it easily by: 您可以通过以下方式轻松管理它:

  1. Creating a Currency class (datatype) 创建货币类(数据类型)
  2. Storing the currencies in a Map instead of a List 将货币存储在Map而不是List

Given below is the sample code: 下面是示例代码:

import java.util.HashMap;
import java.util.Map;

class Currency {
    String code;
    double convertionRate;
    String symbol;

    Currency(String code, double convertionRate, String symbol) {
        this.code = code;
        this.convertionRate = convertionRate;
        this.symbol = symbol;
    }

    public double getConvertionRate() {
        return convertionRate;
    }

    public void setConvertionRate(double convertionRate) {
        this.convertionRate = convertionRate;
    }

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
}

public class Main {
    public static void main(String[] args) {
        Map<String, Currency> currencyMap = new HashMap<String, Currency>();
        // Populate currencyMap with values read from the .txt file e.g.
        currencyMap.put("USD", new Currency("USD", 1.60, "$"));
        // Replace the above line with currencyMap.put(currency_code-from-file,new
        // Currency(currency_code-from-file,conversion_rate-from-file,currency_symbol-from-file));
        // and use it inside the loop which you must be using to read the content from .txt file

        // Getting the corresponding conversion rate and symbol for a currency e.g. USD
        Currency currency = currencyMap.get("USD");
        // Replace the above statement with Currency
        // currency=currencyMap.get(String.valueOf(JComboBox.getSelectedItem()) in your
        // program.
        double convertionRate = currency.getConvertionRate();
        String symbol = currency.getSymbol();
        // Use convertionRate and symbol in your calculation
    }
}

Update: Since you have mentioned in your comment that you are facing difficulty loading the currencies into currencyMap , I have added the following update just to demonstrate how to load the currencies from the .txt file into currencyMap : 更新:由于您在评论中提到您难以将货币加载到currencyMap ,因此我添加了以下更新,只是为了演示如何将货币从.txt文件加载到currencyMap

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

class Currency {
    String code;
    double convertionRate;
    String symbol;

    Currency(String code, double convertionRate, String symbol) {
        this.code = code;
        this.convertionRate = convertionRate;
        this.symbol = symbol;
    }

    public double getConvertionRate() {
        return convertionRate;
    }

    public void setConvertionRate(double convertionRate) {
        this.convertionRate = convertionRate;
    }

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
}

public class Main {
    Map<String, Currency> currencyMap = new HashMap<String, Currency>();

    public static void main(String[] args) {
        Main main = new Main();
        main.populateCurrencyMap();

        // Getting the corresponding conversion rate and symbol for a currency e.g. USD
        Currency currency = main.currencyMap.get("USD");
        if (currency != null)
            System.out.println("Rate: " + currency.getConvertionRate() + ", Symbol: " + currency.getSymbol());
    }

    void populateCurrencyMap() {
        File file = new File(
                "/Users/arvind.avinash/Documents/workspace-spring-tool-suite-4-4.4.0.RELEASE/AdHoc/src/currency2.txt");
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));

            String line = in.readLine().trim();

            while (line != null) {
                String[] parts = line.split(",");
                String currency = parts[0].trim();
                double rate = Double.parseDouble(parts[1].trim());
                String symbol = parts[2].trim();

                currencyMap.put(currency, new Currency(currency, rate, symbol));
                line = in.readLine();
            }

            in.close();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Output: 输出:

Rate: 1.6, Symbol: $

Content of currency2.txt: currency2.txt的内容:

USD, 1.60, $
EUR, 1.41, € 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM