这是我以前做过的家庭作业。就像我尽力而为,并转入我所拥有的并且得到了评分。我的任务是创建一个GUI,让你选择你所拥有的车型(紧凑型,中型,豪华型,SUV),因为这决定了你的油箱大小,你想要使用什么类型的气体(超级无铅,无铅,含铅,柴油)因为它确定了每加仑的价格和你想要旅行的距离。然后它会计算出它的成本。我有计算所有这些的算法,但是我的班级正在教Swing,这对我来说几乎没有任何意义,我决定尝试使用JavaFX重新制作它。我现在的问题是我无法编译项目,因为构造函数存在问题,我不知道如何获取它,因此在按下计算按钮时它使用calculate()方法。我现在不太担心程序的布局,因为我看不到它。我稍后会谈到的。任何人都能帮助我吗?就像我说的,这是一个已经被评分的旧作业,我只想尝试javaFx而不是Swing。
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();
}
}
答案 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();
}