2017-11-10 5 views
0

私はGUIを起動して実行することができたプロジェクトを終了するのに苦労していますが、ボタンを押すと出力がGUIウィンドウに表示されません。私の端末画面に出力が表示されます。組み込み方Else文をJava GUIで出力する場合は?

Screen Cap of GUI

私はGUI画面上で、端末の画面ではなく、ディスプレイに私の出力プリントボタンのいずれかを押す - 私は私が間違っているのかわからないんだけど。あなたの場合/他のコードは正確にあなたが起こることを期待しない何のSystem.out

に印刷されているためである
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 
+0

「EventDriven」の意味を理解する必要があります。 – Sedrick

+1

GUIに情報を表示するには、 'Text'、' Label'、 'TextField'、' TextArea'のいずれかのノードを使用する必要があります。あなたが使用できる他のものがあるかもしれません。結果をポップアップ表示する 'Alert'を作成することができます。 – Sedrick

答えて

1

GUIアプリケーションで何かを印刷する場合は、テキストを「印刷」できるパネル/フレームにコンポーネントを追加する必要があります。

たとえば、テキストエリアまたはラベル。いくつかの静的コンテンツから始めて、そのようなコンポーネントの内容を変更する方法や、フレームを再描画する方法をさまざまな方法で調べることができます。

+0

愚かな自動修正。そこに... Not not three :-) – GhostCat

+0

GhostCatに感謝します。なぜ出力がターミナルウィンドウ、つまりSystem.out.printlnに表示されるのか理解しています。私が理解しようとしているのは、GUIウィンドウに表示する方法です。私はテキストエリアの提案をチェックアウトし、これを理解しようとします。 – Retreever

関連する問題