Я создал класс PizzaMenuView
, содержащий большое количество кода, например, GUI.Я не могу заставить его бежать через главное.По какой-то причине он ничего не показывает, когда я запускаю код.
Может кто-нибудь сказать мне, посмотрев на мои два класса, почему это ничего не показывает.Может быть, я неправильно называю методы?PizzaOrderingSystem
- это мой основной класс, а PizzaMenuView
- это класс, который я хотел бы использовать для своего основного.
public class PizzaOrderingSystem extends Application {
private PizzaMenuView pizzaMenuView;
//Creating Stage and making layout for the stage
@Override
public void start(Stage primaryStage){
pizzaMenuView = new PizzaMenuView();
Scene scene = new Scene(pizzaMenuView, 650, 650);
primaryStage.setTitle("Pizza Ordering System");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
public PizzaMenuView(){
/*
* Creating border1 which contain border2 in the top and grid2 in the center.
* border2 and contains hBoxTop and a grid1.
*/
border1 = new BorderPane();
border1.setStyle("-fx-background-color: lightgrey");
border1.setTop(border2);
border1.setCenter(grid2);
border1.setBottom(hBoxBottom);
border2 = new BorderPane();
// Attaching hbox, vboxes and grid to border2.
border2.setTop(hBoxTop);
border2.setCenter(grid1);
hBoxTop = new HBox();
titel = new Text("Dortes pizza place");
/**
* Layout settings for the titel in the hBoxTop
* Setting the linear gradient to go from lightblue to darkblue
*/
Stop[] stops = new Stop[] {
new Stop(0, Color.LIGHTBLUE),
new Stop(1, Color.DARKSLATEBLUE)
};
LinearGradient linearGradient =
new LinearGradient(0, 1, 1, 0, true, CycleMethod.NO_CYCLE, stops);
// Adding the titel text to hBoxTop and selecting font, gradient color, and letter size.
hBoxTop.getChildren().addAll(titel);
titel.setFont(Font.font ("Verdana", 50));
titel.setFill(Color.LIGHTBLUE);
titel.setStrokeWidth(2);
titel.setStroke(Color.DARKSLATEBLUE);
titel.setFill(linearGradient);
// Making layout for hBoxTop
hBoxTop.setAlignment(Pos.CENTER);
hBoxTop.setPadding(new Insets(5, 30, 30, 5));
hBoxTop.setMaxHeight(30);
hBoxTop.setPrefHeight(30);
hBoxTop.setFillHeight(false);
// creating grid1 that is in the bottom of border1
grid1 = new GridPane();
// Styling the three columns for grid1.
titelCategoryColumn = new ColumnConstraints();
titelCategoryColumn.setPercentWidth(40);
titelCategoryColumn.setHalignment(HPos.LEFT);
titelNameColumn = new ColumnConstraints();
titelNameColumn.setPercentWidth(40);
titelNameColumn.setHalignment(HPos.LEFT);
titelPriceColumn = new ColumnConstraints();
titelPriceColumn.setPercentWidth(40);
titelPriceColumn.setHalignment(HPos.LEFT);
// Adding columns to grid1
grid1.getColumnConstraints().add(titelCategoryColumn);
grid1.getColumnConstraints().add(titelNameColumn);
grid1.getColumnConstraints().add(titelPriceColumn);
//Styling and adding the row to the grid1
row1 = new RowConstraints();
row1.setPrefHeight(30);
row1.setValignment(VPos.CENTER);
row1.setVgrow(Priority.ALWAYS);
grid1.getRowConstraints().add(row1);
// Making text to add to grid1
Text text1 = new Text("Category");
text1.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));
Text text2 = new Text("Menu item");
text2.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));
Text text3 = new Text("Price");
text3.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));
// Adding text to grid1
grid1.add(text1, 1, 0);
grid1.add(text2, 2, 0);
grid1.add(text3, 3, 0);
/*
NOW TO BORDER1
*/
// creating grid2 that is in the center of border1
grid2 = new GridPane();
// Styling the column for the grid2
checkBoxColumn = new ColumnConstraints();
checkBoxColumn.setPercentWidth(20);
checkBoxColumn.setHalignment(HPos.CENTER);
categoryColumn = new ColumnConstraints();
categoryColumn.setPercentWidth(40);
categoryColumn.setHalignment(HPos.LEFT);
nameColumn = new ColumnConstraints();
nameColumn.setPercentWidth(40);
nameColumn.setHalignment(HPos.LEFT);
priceColumn = new ColumnConstraints();
priceColumn.setPercentWidth(40);
priceColumn.setHalignment(HPos.LEFT);
//styling rows for grid2.
row2 = new RowConstraints();
row2.setPrefHeight(100);
row2.setValignment(VPos.CENTER);
row2.setVgrow(Priority.ALWAYS);
// Getting and defining arrayList with menuItems from the Class OrderMenu
menu = new OrderMenu();
ArrayList<MenuItem> menuItems = menu.menuItems;
// Adding columns to grid2
grid2.getColumnConstraints().add(checkBoxColumn);
grid2.getColumnConstraints().add(categoryColumn);
grid2.getColumnConstraints().add(nameColumn);
grid2.getColumnConstraints().add(priceColumn);
/**
* For loop gets size of arrayList and diving them into objects
* which makes it possible for them to be assigned to the grid.
*/
for (int item = 0; item < menuItems.size(); item++) {
// Set the styling for row2
grid2.getRowConstraints().add(row2);
// Get the required info out of each item
String category = menuItems.get(item).category;
String name = menuItems.get(item).name;
double price = menuItems.get(item).price;
// Add the text to the grid2
Text categoryText = new Text(category);
categoryText.setFont(Font.font("verdana", FontPosture.REGULAR, 15));
Text nameText = new Text(name);
nameText.setFont(Font.font("verdana", FontPosture.REGULAR, 15));
Text priceText = new Text(Double.toString(price) + " DKK");
priceText.setFont(Font.font("verdana", FontPosture.REGULAR, 15));
CheckBox check = new CheckBox ();
check.setStyle("-fx-focused-color: lightblue");
// Adding columns to grid1
grid2.add(check, 0, item);
grid2.add(categoryText, 1, item);
grid2.add(nameText, 2, item);
grid2.add(priceText, 3, item);
hBoxBottom = new HBox();
// Making layout for hBoxBottom.
hBoxBottom.setAlignment(Pos.BASELINE_RIGHT);
hBoxBottom.setPadding(new Insets(20, 50, 50, 50));
hBoxBottom.setMaxHeight(70);
hBoxBottom.setPrefHeight(70);
hBoxBottom.setFillHeight(false);
Button button = new Button("Add selected items to basket");
button.setFont(Font.font("verdana", FontPosture.REGULAR, 15));
button.setStyle("-fx-background-color: LIGHTBLUE");
button.setMinHeight(50);
hBoxBottom.getChildren().add(button);
}
}
}