获取一个视图类以显示在main上并与该视图类的内容一起运行

时间:2018-12-10 17:37:54

标签: java javafx main

我制作了一个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);

        }
    }

}

2 个答案:

答案 0 :(得分:0)

如我所见,您可以在PizzaMenuView的构造函数中创建所有GUI项(最好以更易于处理的方式组织代码),然后使用main方法这个:

this.pizzaMenuView.border1 = new BorderPane();

也就是说,您创建一个BorderPane的新实例并将其存储在this.pizzaMenuView.border1中,从而覆盖当前引用。

只需尝试注释掉该行。

编辑

尝试以下操作:

Scene scene = new Scene(pizzaMenuView.border1, 650, 650);

原因是Scene constructor期望使用类型为Parent的参数,该参数是可以在用户界面中显示的节点,而pizzaMenuView不是节点(请参见documentation

答案 1 :(得分:0)

如果我理解了您的问题“我无法通过主程序运行此问题” ,那么您想在PizzaMenuView()内创建PizzaOrderingSystem.main()的实例。 / p>

您似乎在PizzaOrderingSystem的 main方法中所做的事情称为launch()方法,该方法是PizzaOrderingSystem()类从其父类继承的方法

  

父类是Application()PizzaOrderingSystem()从此扩展。

您可以找到hereApplication()的Java文档。或单击here,以简单了解launch(String... args)的功能。

因此,要创建PizzaMenuView()的实例,您需要执行以下操作:

public static void main(String[] args){
    //Instance of PizzaMenuView()
    PizzaMenuView menu = new PizzaMenuView();
}

注意: 您已经在PizzaMenuView()中创建了PizzaOrderingSystem.start()的实例,但是由于我不知道您的程序如何工作,因此,我指出了最明显的方法来运行所需类的实例(考虑到您在PizzaMenuView()的构造函数中初始化了所有内容。