2013-11-21 62 views
1

这是我以前完成的一项家庭作业任务。就像我尽力做到的那样,把我所有的东西都交给了我,并把它们分级了。我的任务是创建一个图形用户界面,它可以让你选择你所使用的车型(紧凑,中型,豪华,SUV),因为它决定了你的油箱的尺寸,你想使用什么类型的汽油(超级无铅汽油,无铅,含铅,柴油),因为这决定了每加仑的价格和你想要旅行的距离。然后它会计算出它需要多少成本。我有算法来计算所有这些,但是我的班级正在教Swing,这对我来说毫无意义,我决定尝试使用JavaFX重新制作。我现在的问题是,因为构造函数存在问题,所以无法编译项目,我不知道如何获取它,因此在按下calculate按钮时它使用calculate()方法。我并不担心这个程序的布局,因为我看不到它。我会在稍后谈谈。任何人都能够帮助我?就像我说的,这是一个旧的家庭作业,已经被分级了,我只想尝试javaFx而不是Swing。Javafx没有显示

import javafx.collections.FXCollections; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.scene.control.*; 
import javafx.scene.layout.*; 
import javafx.scene.text.*; 

/** 
* Created with IntelliJ IDEA. 
* User: Zane Erickson 
* Date: 11/16/13 
* Time: 8:27 PM 
* Purpose: Be able to enter estimated miles to drive, gas type, and car type into the GUI to get an estimated cost for travel. 
*/ 

public class TGC extends ButtonBase { 

    private boolean clearText; 

    private double totalCost; 

    private final double SUPERUNLEADED = 3.00; 
    private final double UNLEADED = 2.90; 
    private final double LEADED = 2.50; 
    private final double DIESEL = 4.00; 
    private final double OILCHANGEPRICE = 30.00; 

    private final int COMPACT = 13; 
    private final int MIDSIZE = 18; 
    private final int LUXURY = 15; 
    private final int SUV = 23; 
    private final int OILCHANGEDISTANCE = 3000; 
    private final int MPG = 15; 

    public TGC() 
    { 
     BorderPane border = new BorderPane(); 
     HBox hTitle = addHBox(); 
     border.setTop(hTitle); 
     border.setLeft(addVBox()); 

     border.setCenter(addGridPane()); 
     //border.setRight(addFlowPane()); 
    } 

    @Override 
    public void fire() { 
     calculate(); 
    } 

    public HBox addHBox() 
    { 
     HBox hTitle = new HBox(); 
     hTitle.setPadding(new Insets(15,12,15,12)); 
     hTitle.setSpacing(10); 
     hTitle.setStyle("-fx-background-color: #336699;"); 

     //Add buttons 
     Button buttonCalculate = new Button("Calculate"); 
      buttonCalculate.setPrefSize(100,20); 
     Button buttonReset = new Button("Reset"); 
      buttonReset.setPrefSize(100,20); 

     //Add the title 
     Text txtTitle = new Text("TGC"); 
     txtTitle.setFont(Font.font("Arial", FontWeight.BOLD, 20)); 

     //Add labels 
     Label lblEGasCost = new Label("Estimated Gas Cost: $"); 
     Label lblEOilCost = new Label("Estimated Oil Cost: $"); 
     Label lblETotalCost = new Label("Estimated Total Cost: $"); 

     //Add textFields 
     final TextField txfDistance = new TextField(); 

     //Set lblTitle to top of pane 
     hTitle.getChildren().addAll(txtTitle); 

     buttonCalculate.setOnAction(new EventHandler<ActionEvent>() { 
      @Override 
      public void handle(ActionEvent actionEvent) { 

       double oilTemp = (Integer.valueOf(txfDistance.getText()) % OILCHANGEDISTANCE) * OILCHANGEPRICE; 
       /* 
       if(== "Compact") 
       { 
        if(arg2 == "Super Unleaded") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * COMPACT) * SUPERUNLEADED) + oilTemp; 
        } 
        else if(arg2 == "Unleaded") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * COMPACT) * UNLEADED) + oilTemp; 
        } 
        else if(arg2 == "Leaded") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * COMPACT) * LEADED) + oilTemp; 
        } 
        else if (arg2 == "Diesel") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * COMPACT) * DIESEL) + oilTemp; 
        } 
        else 
        { 

        } 
       } 

       else if(arg == "Mid-Size") 
       { 
        if(arg2 == "Super Unleaded") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * MIDSIZE) * SUPERUNLEADED) + oilTemp; 
        } 
        else if(arg2 == "Unleaded") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * MIDSIZE) * UNLEADED) + oilTemp; 
        } 
        else if(arg2 == "Leaded") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * MIDSIZE) * LEADED) + oilTemp; 
        } 
        else if (arg2 == "Diesel") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * MIDSIZE) * DIESEL) + oilTemp; 
        } 
        else 
        { 

        } 
       } 

       else if(arg == "Luxury") 
       { 
        if(arg2 == "Super Unleaded") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * LUXURY) * SUPERUNLEADED) + oilTemp; 
        } 
        else if(arg2 == "Unleaded") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * LUXURY) * UNLEADED) + oilTemp; 
        } 
        else if(arg2 == "Leaded") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * LUXURY) * LEADED) + oilTemp; 
        } 
        else if (arg2 == "Diesel") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * LUXURY) * DIESEL) + oilTemp; 
        } 
        else 
        { 

        } 
       } 

       else if(arg == "SUV") 
       { 
        if(arg2 == "Super Unleaded") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * SUV) * SUPERUNLEADED) + oilTemp; 
        } 
        else if(arg2 == "Unleaded") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * SUV) * UNLEADED) + oilTemp; 
        } 
        else if(arg2 == "Leaded") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * SUV) * LEADED) + oilTemp; 
        } 
        else if (arg2 == "Diesel") 
        { 
         totalCost = ((Integer.valueOf(txfDistance.getText()))/(MPG * SUV) * DIESEL) + oilTemp; 
        } 
        else 
        { 

        } 
       } 
       else 
       { 

       } 
       totalPrice.setText((String.valueOf(totalCost))); 
       */ 
      } 

     }); 

     buttonReset.setOnAction(new EventHandler<ActionEvent>() { 
      @Override 
      public void handle(ActionEvent actionEvent) { 
       reset(); 
      } 
     }); 

     return hTitle; 
    } 

    public VBox addVBox() 
    { 
     VBox vbox = new VBox(); 
     vbox.setPadding(new Insets(10)); 
     vbox.setSpacing(8); 

     ChoiceBox chbCarType = new ChoiceBox(); 
      chbCarType.setItems(FXCollections.observableArrayList("Compact", "Mid-Size", "Luxury", "SUV")); 
      chbCarType.setTooltip(new Tooltip("Select Vechicle Type")); 
     ChoiceBox chbGasType = new ChoiceBox(); 
      chbGasType.setItems(FXCollections.observableArrayList("Super Unleaded", "Unleaded", "Leaded", "Diesel")); 
      chbGasType.setTooltip(new Tooltip("Select Fuel Type")); 

     vbox.getChildren().addAll(chbCarType, chbGasType); 

     return vbox; 
    } 

    public GridPane addGridPane() 
    { 
     GridPane grid = new GridPane(); 
     grid.setHgap(10); 
     grid.setVgap(10); 
     grid.setPadding(new Insets(0,10,0,10)); 

     return grid; 
    } 

    public void calculate() 
    { 

     //code is currently in buttonCalculate.setOnAction() 
    } 

    public void reset() 
    { 
     clearText = true; 
    } 
} 


/////////////////////////End of Class//////////////////////////// 

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

public class Main extends Application { 

    public static void main(String[] args) 
    { 
     SplashScreen.createSplashScreen(); //call createSplashScreen in the SplashScreen class 
     launch(args); 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 
     stage.setTitle("TGC"); 
     TGC f = new TGC(); //not sure how to use f so that I call TGC properly 
     Group root = new Group(); 
     Scene scene = new Scene(root, 800, 600, Color.BLACK); 
     stage.setScene(scene); 
     stage.show(); 
    } 
} 
+1

的JavaFX提供FXML这是设计来控制和使用它......你可以使用控制器报送容易realiable ...试试这个http://stackoverflow.com/questions/19523341/adding-a-tilepane-instantiated-in-java-files-to-fxml –

+0

哪一行不编译,错误信息是什么? –

回答

0

从你的TGC类中移除BorderPane和所有那些东西,因为这个类扩展了ButtonBase。 你需要向下移动一些的东西到你的启动方法...

@Override 
    public void start(Stage stage) throws Exception { 
     stage.setTitle("TGC"); 
     BorderPane root = new BorderPane(); 
     hTitle = addHBox(); 
     border.setTop(hTitle); 
     border.setLeft(addVBox()); 

     border.setCenter(addGridPane()); 
     Scene scene = new Scene(root, 800, 600, Color.BLACK); 
     stage.setScene(scene); 
     stage.show(); 
    }