gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:34:32 26 4
gpt4 key购买 nike

为什么我无法使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com