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



我目前正在尝试创建一个GUI,该GUI从包含汇率的.txt文件中馈送信息。

文本文件包含 3 个不同的值,用","分隔。

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

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

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

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

例如,如果用户选择USD(arrayCurrency(并将5输入JTextField,则程序需要选择正确的汇率(arrayRate(。

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

  1. 创建货币类(数据类型(
  2. 将货币存储在Map而不是List

下面是示例代码:

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

更新:由于您在评论中提到您在将货币加载到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());
}
}
}

输出:

Rate: 1.6, Symbol: $

货币含量2.txt:

USD, 1.60, $
EUR, 1.41, € 

但是,我现在需要程序来选择正确的汇率 和基于用户选择的货币名称的货币符号 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
}

现在,您已经知道了检查是否应该以某种汇率和某种符号显示所选货币所需的一切。

最新更新