繁体   English   中英

如何使用 JavaFX 创建第二个列表和反向按钮以在我的货币转换器中的货币之间进行交换? [关闭]

[英]How do I create a second list and a reverse button to interchange between the currencies in my currency converter using JavaFX? [closed]

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

package currencyconverter;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.Event;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;


public class Main extends Application {

    @Override
    public void start(Stage appStage) throws Exception {
        
        Scene scene = new Scene(new Main().dataCollectionDialogue(),473,315);
        appStage.setScene(scene);
        appStage.setTitle("Currency converter");
        appStage.show();//launch the application

    }


    public Pane dataCollectionDialogue(){
        Pane pane =  new Pane();
        Label label = new Label("Convert from Euros");
        label.setLayoutY(40);
        label.setLayoutX(120);

        TextField amount = new TextField();
        amount.setPromptText("Enter the amount to convert");
        amount.setLayoutY(69);
        amount.setLayoutX(133);
        amount.setPrefWidth(100);

        Button btn = new Button("Convert");
        btn.setLayoutY(270);
        btn.setLayoutX(195);

        ComboBox<currencies> countriesComboBox = new ComboBox<>();
        
        countriesComboBox.setItems(FXCollections.observableArrayList(currencies.values()));
        countriesComboBox.setPromptText("Select a Currency");
        countriesComboBox.setLayoutY(165);
        countriesComboBox.setLayoutX(133);

        
        btn.setOnAction(event -> {
           
           String countryName= countriesComboBox.getValue().toString();
           
            double m = Double.parseDouble(amount.getText());

            double convert = currencyCalculator(countryName, m);
            String message = "The converted amount is : "+convert;

            Stage stage = new Stage();
            Scene scene = new Scene(getConvertedAmountWindow(message));
            stage.setScene(scene);
            stage.setTitle("results");
            stage.show();
        });

        pane.getChildren().addAll(label, amount,btn,countriesComboBox);
        return pane;

    }

    
    private Pane getConvertedAmountWindow( String toDisplay){

        Pane pane = new Pane();
        pane.setPrefSize(473, 117);
        Label label = new Label();
        label.setLayoutX(121);
        label.setLayoutY(50);
        label.setText(toDisplay);

        pane.getChildren().addAll(label);
        return pane;
    }

  
    private double currencyCalculator(String currency, double amountTocConvert){
        double amount = 0;
        //currency conversion selection process
        switch (currency){
            case "USD":
                amount = amountTocConvert*1.071970;
                break;
            case "GBP":
                amount = amountTocConvert*0.858277;

        }

        return amount;
    }

  
    enum currencies{
        USD,
        GBP

    }
}   

我想创建第二个列表来保存货币和在它们之间反转的按钮。 第一个列表应该像第二个列表一样包含 USD、EUR、GBP。 反向按钮将在列表之间互换。

现在我只能从欧元转换,我希望能够在任何货币之间转换。

暂无
暂无

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

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