簡體   English   中英

Java:將ArrayList與JComboBox和JTextField結合使用

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

我目前正在嘗試創建一個GUI,該GUI從包含匯率的.txt文件中獲取信息。

文本文件包含3個不同的值,並用','分隔。

值是:貨幣名稱,匯率,貨幣符號。

我正在運行程序,以便將這3個值存儲到3個不同的ArrayLists中

該程序還將使用貨幣名稱更新JComboBox。

但是,我現在需要該程序根據用戶在JComboBox中選擇的貨幣名稱來選擇正確的匯率和貨幣符號。

例如,如果用戶選擇USD(arrayCurrency)並將5輸入到JTextField中,則程序需要選擇正確的匯率(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
}

現在,您有了所有需要檢查的想法,以檢查是否應顯示某些匯率和符號的貨幣。

您可以通過以下方式輕松管理它:

  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: $

currency2.txt的內容:

USD, 1.60, $
EUR, 1.41, € 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM