如何在Java GUI中合并If Else语句输出?

时间:2017-11-10 15:22:50

标签: java javafx

我正在努力完成一个项目,我已经设法让我的GUI启动并运行,但是当我按下其中一个按钮时,我无法在GUI窗口上显示输出 - 现在当我按下按钮输出显示在我的终端屏幕上。

Screen Cap of GUI

当我按下任一按钮时,我的输出打印到终端屏幕而不是显示在GUI屏幕上 - 我不确定我做错了什么。
Screen Cap of Terminal Output when I push either of the buttons
Command window output

import java.text.NumberFormat;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.text.Text;
import javafx.scene.layout.FlowPane;
import javafx.geometry.Pos;
import javafx.event.ActionEvent;


    public class CarRentalDriverGui extends Application {

        private Button economyButton;
        private Button businessButton;
        private TextField capacityField;


        /**Specified number formatter for currency formatting on cost .*/
        NumberFormat formatter = NumberFormat.getCurrencyInstance();

            public void start (Stage primaryStage) {
                primaryStage.setTitle ("Vehicle Rental Contract Generator");

                Label fieldLabel = new Label ("Enter the capacity of the vehicle required then select Economy or Business button");
                capacityField = new TextField ();
                capacityField.setPrefWidth (50);
                capacityField.setOnAction(this::processRentalRequest);
                economyButton = new Button ("Economy Rental");
                economyButton.setOnAction
                (this::processRentalRequest);
                businessButton = new Button ("Business Rental");
                businessButton.setOnAction
                (this::processRentalRequest);
                FlowPane pane = new FlowPane(fieldLabel, capacityField, economyButton, businessButton);
                pane.setAlignment(Pos.CENTER);
                pane.setHgap (40);
                Scene scene = new Scene (pane, 600, 200);
                primaryStage.setScene (scene);
                primaryStage.show ();
                }

            /** Method for creating test rental contracts*/
            public void processRentalRequest (ActionEvent event) {
                int capacity = Integer.parseInt(capacityField.getText());
                //double economyResult = getDailyRentalCost();
                //double businessResult = getDailyRentalCost();     
                //create economy rental contract
                if (event.getSource() == economyButton) {

                    EconomyRentalContract Test1 = new EconomyRentalContract(capacity);
                    //economyResult.setText("Rate: "+ formatter.format(Test1.getDailyRentalCost()));
                    System.out.println ("Rate: "+ formatter.format(Test1.getDailyRentalCost()));
                    System.out.println ("Insurance Cost: "+ formatter.format(Test1.dailyInsuranceCost()));
                    System.out.println ("Total Cost: " + formatter.format(Test1.getDailyRentalCost() + Test1.dailyInsuranceCost()));
                    }
                    else {
                    //economyResult.setText("Rate: "+ formatter.format(Test2.getDailyRentalCost()));
                    BusinessRentalContract Test2 = new BusinessRentalContract(capacity);
                    System.out.println ("Rate: "+ formatter.format(Test2.getDailyRentalCost()));
                    System.out.println ("Rewards Points: " + Test2.rewardPointsEarned());
                    }
                    //System.out.println("Enter customer type \n1. Business \n2. Economy: ");
                     //customerType = userIn.nextInt();
            }
                    //System.out.print("Invalid number please enter 1 or 2! ");


                //counter.setText ("" + number);
                //Button convertButton = new Button ("Convert to Fahrenheit");
                //convertButton.setOnAction (this::processConvertRequest);
                //fahrenheitResult = new Text ("Welcome to my temperature converter!");
} //end class

1 个答案:

答案 0 :(得分:1)

这是因为您的if / else代码正在打印到System.out

您期望到底发生什么?

如果您想通过GUI应用程序打印某些内容,则必须在面板/框架中添加一个能够打印"文本。

例如textarea或标签。您可以从一些静态内容开始 - 然后研究如何更改此类组件的内容以及如何让您的框架重绘自身的各种方法。