java - 第二次为 Java 创建 MVC 或 MVP 模型的失败尝试 - getChildren().add 出现意外错误

标签 java eclipse javafx

为什么我无法使用 getChildren.add() 添加标签和文本字段?我以前使用过这种方法,但我不确定是什么导致它在这个项目中失败。我正在尝试使用 MVC 或 MVP 来创建这个,但我过去很难实现。 MVP 可能是我困惑的根源:

GUI

//错误第 76 行。vBox.getChildren.add(amountOfMoneyLbl) 上的错误; //文件:GraphicalInterface.java

//this is the package that the view is created inside of
package com.mvc.view;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Label;
import java.awt.TextField;

//How to create multiple packages: https://www.youtube.com/watch?v=ZE6gdLZydDc
import com.mvc.model.Model;
//provides the control libraries for buttons, textfields, etc.
import javafx.scene.*;
import javafx.application.*;
import javafx.geometry.Insets;
//http://stackoverflow.com/questions/25222811/access-restriction-the-type-application-is-not-api-restriction-on-required-l
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;
//This file contains the View portion of the project Finance Calculator

public class GraphicalInterface extends BorderPane
{
    private final Model myModel;



    public GraphicalInterface(Model model) 
    {
        this.myModel = model;
        //this.createView();


    }

    @SuppressWarnings("restriction")
    public void createView(Stage primaryStage)
    {
        //Labels
        Label amountOfMoneyLbl = new Label("Amount of money in cents: ");
        Label aprLbl = new Label("APR: ");
        Label numPaymentsLbl = new Label("Number of payments: ");
        Label paymentLbl = new Label("Payment: ");

        //Text Fields
        TextField amountOfMoneyTextFld = new TextField("");
        TextField aprTextFld = new TextField("");
        TextField numPaymentsTextFld = new TextField("");
        TextField paymentTextFld = new TextField("");

        //Button
        Button calcBtn = new Button("Calculate");


        BorderPane myBorderPane = new BorderPane();

        myBorderPane.setVisible(true);


        Scene scene = new Scene(myBorderPane, 600, 400);

        //SET SIZE OF SCENE
        //parameters on color are r, g, b, float opacity?
        primaryStage.setScene(scene);
        primaryStage.setTitle("Finance calculator");
        primaryStage.show();

        VBox vBox = new VBox(15);
        vBox.setVisible(true);
        vBox.setPadding(new Insets(15, 15, 15, 15));      
        vBox.setStyle("-fx-background-color:black");

        //THIS SHOULD WORK, BUT DOESN'T - WHO ARE THE CHILDREN?
        vBox.getChildren.add(amountOfMoneyLbl);

        myBorderPane.setCenter(vBox);


        //myBorderPane.getChildren().addAll();
        //myBorderPane.getChildren().add(calcBtn);
//      
//      this.add(amountOfMoneyLbl);
//      this.add(amountOfMoneyTextFld);
//      
//      this.add(aprLbl);
//      this.add(aprTextFld);
//      
//      this.add(numPaymentsLbl);
//      this.add(numPaymentsTextFld);
//      
//      this.add(paymentLbl);
//      this.add(paymentTextFld);
//      
//      this.add(calcBtn);

        //set prompt for user input in the first textfield
//      amountOfMoneyTextFld.setPromptText();

        //bind fields to model here.....page 429

    }

    public void bindFieldsToModel()
    {

    }


}
///////////////////////////////////////////////////////////////////
// File: myApp.java
///////////////////////////////////////////////////////////////////
package com.mvc.model;

import com.mvc.view.GraphicalInterface;
import com.mvc.view.Presenter;
import com.mvc.model.Model;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import javafx.scene.layout.VBox;

public class myApp extends Application{

    public static void main(String[] args)
    {
        //this method is from inside the Application class.
        //It sets up your program as a javaFx Application, then calls start().

        // Application.launch() -> Application.start()

        Application.launch(args);

    }

    //The primaryStage is the main window.
    @Override
    public void start(Stage primaryStage) throws Exception {


        Model model = new Model();


        //The window is called the stage. The stuff inside the 
        //window is called the scene.

        //This view object creates the stage and the scene.

        GraphicalInterface view = new GraphicalInterface(model);

        view.createView(primaryStage);

        //create scene first because the presented uses the scene to listen
        //Scene scene = new Scene(view);

        Presenter p = new Presenter(model, view);

    }

}

最佳答案

您的代码中(至少)有两个错误。

而不是

        vBox.getChildren.add(amountOfMoneyLbl);

应该是

        vBox.getChildren().add(amountOfMoneyLbl);

并且您为 amountOfMoneyLbl 导入了错误的 (AWT) 标签类。

关于java - 第二次为 Java 创建 MVC 或 MVP 模型的失败尝试 - getChildren().add 出现意外错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43427936/

相关文章:

java - 为什么我不能在 Java 的内部类中创建枚举?

JavaFX 绑定(bind)到多个属性

javafx - 如何将警报设置为始终在最前面?

java - 结束阻塞的 GUI 线程

java - 双重检查锁定延迟初始化 - 我是否需要为 Map 使用 volatile 关键字

java - MIDI 乐器在导出到 .jar 文件时听起来不同

java - JRE系统库在Eclipse中总是未绑定(bind)? (: "Launching Main".模型不适用于helloWorld期间发生内部错误)

eclipse - 使用Lombok的@ Slf4j和Eclipse构建:找不到符号日志

JavaFX刷新方法内的进度条

java - SwingUtilities.getLocationOnScreen() 的问题;