Java:将ArrayList与JComboBox和JTextField一起使用

Java:将ArrayList与JComboBox和JTextField一起使用,java,Java,我目前正在尝试创建一个GUI,它从一个包含汇率的.txt文件中获取信息 文本文件包含3个不同的值,它们用“,”分隔 值为:货币名称、汇率、货币符号 我让程序运行,以便将这3个值存储到3个不同的ArrayList中 该程序还使用货币名称更新JComboBox 但是,我现在需要程序根据用户在JComboBox中选择的货币名称选择正确的汇率和货币符号 例如,如果用户选择美元(arrayCurrency)并在JTextField中输入5,则程序需要选择正确的汇率(arrayRate) 但是,我现在需要程

我目前正在尝试创建一个GUI,它从一个包含汇率的.txt文件中获取信息

文本文件包含3个不同的值,它们用“,”分隔

值为:货币名称、汇率、货币符号

我让程序运行,以便将这3个值存储到3个不同的ArrayList中

该程序还使用货币名称更新JComboBox

但是,我现在需要程序根据用户在JComboBox中选择的货币名称选择正确的汇率和货币符号

例如,如果用户选择美元(arrayCurrency)并在JTextField中输入5,则程序需要选择正确的汇率(arrayRate)

但是,我现在需要程序来选择正确的汇率 和基于用户在中选择的货币名称的货币符号 JComboBox

这不完全是个问题。。。我假设你要求的是:

如何从JComboBox中获取值?->
yourJCBox.getSelectedItem()

现在您有了这个字符串-您需要决定如何将汇率映射到美元金额。无论是2d、3d阵列还是您设计的对象:

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

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

  // Getters and setters
}
现在,您已经了解了检查所选货币是否应显示某些汇率和符号所需的所有操作

但是,我现在需要程序来选择正确的汇率 和基于用户在中选择的货币名称的货币符号 JComboBox

这不完全是个问题。。。我假设你要求的是:

如何从JComboBox中获取值?->
yourJCBox.getSelectedItem()

现在您有了这个字符串-您需要决定如何将汇率映射到美元金额。无论是2d、3d阵列还是您设计的对象:

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

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

  // Getters and setters
}

现在,您已经了解了检查所选货币是否应显示某些汇率和符号所需的一切操作。

您可以通过以下方式轻松管理:

  • 创建货币类(数据类型)
  • 将货币存储在
    地图
    而不是
    列表
  • 以下是示例代码:

    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
        }
    }
    
    输出:

    Rate: 1.6, Symbol: $
    
    USD, 1.60, $
    EUR, 1.41, € 
    
    currency2.txt的内容:

    Rate: 1.6, Symbol: $
    
    USD, 1.60, $
    EUR, 1.41, € 
    

    您可以通过以下方式轻松管理它:

  • 创建货币类(数据类型)
  • 将货币存储在
    地图
    而不是
    列表
  • 以下是示例代码:

    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
        }
    }
    
    输出:

    Rate: 1.6, Symbol: $
    
    USD, 1.60, $
    EUR, 1.41, € 
    
    currency2.txt的内容:

    Rate: 1.6, Symbol: $
    
    USD, 1.60, $
    EUR, 1.41, € 
    


    别这样。这是一个可以轻松映射到类的CSV文件。该类应保存一行的所有值。让生活更轻松我如何实现这个货币类?我是否应该读取Currency类中的文件并将其存储在那里的新数组中。这是一个可以轻松映射到类的CSV文件。该类应保存一行的所有值。让生活更轻松我如何实现这个货币类?我是否应该读取Currency类中的文件并将其存储在那里的新数组中?这实际上是一个问题,只是一个完全错误的入口点。将CSV映射到POJO应该是Gojo的方法。使用一个正在更改的.txt,这是否有效?货币名称并不总是相同的。您必须解析.txt文件并根据从.txt文件中获取的数据动态创建对象。这实际上是一个问题,只是一个完全错误的入口点。将CSV映射到POJO应该是Gojo的方法。使用一个正在更改的.txt,这是否有效?货币名称并不总是相同的。您必须解析.txt文件并根据从.txt文件中获取的数据动态创建对象。每次程序启动时,如何使用.txt自动执行此操作?货币名称和汇率可能因使用的.txt文件而异。我创建了一个新类并添加了货币代码。在我的while循环中,我添加了
    currencyMap.put(currency,newcurrency(currency,rateConverted,symbol))如何在组合框中显示?我仍然需要帮助。它似乎没有在调试中向映射添加任何内容。这是.txt文件的一行。欧元(EUR),1.41,高欧元。每次程序启动时,如何使用.txt自动执行此操作?货币名称和汇率可能因使用的.txt文件而异。我创建了一个新类并添加了货币代码。在我的while循环中,我添加了
    currencyMap.put(currency,newcurrency(currency,rateConverted,symbol))如何在组合框中显示?我仍然需要帮助。它似乎没有在调试中向映射添加任何内容。这是.txt文件的一行。欧元,1.41欧元