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

标签 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

最佳答案

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

您到底希望发生什么?

如果您想通过 GUI 应用程序打印某些内容,那么您必须向面板/框架添加一个能够“打印”文本的组件。

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

关于java - 如何在 Java GUI 中合并 If Else 语句输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47226051/

相关文章:

javafx-2 - 阻塞父阶段直到子阶段关闭

java - JavaFx 中标签上带有 setText 的性能问题

java - 从 Throwable 获取根错误消息

java - aapt 使用 Runtime.exec()

Javafx 报告工具解决方案

binding - 是否可以使用 JavaFX HTMLEditor 的绑定(bind)

JavaFX:双向绑定(bind)的初始值

java - 第一次尝试后 TCP 套接字通信失败

java - 查询检查列表内部

java - JLabel到JPanel,拖放