java - 如何为移动应用程序设置 init 方法

标签 java javafx

为了更好的解释它在这里。我最近开始使用 Gluon 创建移动应用程序,在创建 MainMenu.java 文件时遇到了一些问题。我无法配置 init 方法并不断收到“字符串无法转换为阶段”之类的错误,并且不确定如何解决此问题。非常感谢您的帮助,我不确定如何继续。我不断收到错误:

error: incompatible types: invalid constructor reference
 addViewFactory(HOME_VIEW, LoginPage::new);
                                  ^
constructor LoginPage in class LoginPage cannot be applied to given types required: Stage
 found: no arguments
 reason: actual and formal argument lists differ in length

我尝试更改 addViewFactory 方法,但没有任何效果,包括将 PrimaryStage 放在最后一个 () 内。

这是我遇到问题的文件

public class AlexDemo extends MobileApplication {

    public static final String LOGIN_VIEW = HOME_VIEW;

    @Override
    public void init() {

            addViewFactory(LOGIN_VIEW, () -> new LoginPage(LOGIN_VIEW));    

    }

    @Override
    public void postInit(Scene scene) {
        Swatch.BLUE.assignTo(scene);

        ((Stage) scene.getWindow()).getIcons().add(new Image(AlexDemo.class.getResourceAsStream("/icon.png")));
    }
}

这是带有有效代码的登录页面(只显示了一些我有太多的按钮、标签等)

public class LoginPage extends View {

    private Parent root;

    public LoginPage(Stage primaryStage) {

        Scene scene = new Scene(root, 500, 800);
        BorderPane root = new BorderPane();

        //For the label displaying "Sign In" to prompt the user
        VBox login = new VBox();
        Label statement = new Label("Sign In");
        statement.setFont(Font.font("Verdana", FontWeight.BOLD, 13));

        //HBox for the email the user wants to sign in with
        HBox firstUserPrompt = new HBox();
        Label email = new Label("Username/\nEmail: ");
        email.setFont(Font.font("Verdana", 13));
        firstUserPrompt.setAlignment(Pos.CENTER);
        TextField userPrompt = new TextField();
    }

    @Override
    protected void updateAppBar(AppBar appBar) {

        appBar.setTitleText("Book App");
        appBar.setSpacing(115);

    }

}

这是单击按钮后的主菜单页面

public class MainMenu extends View{

    private Parent prevPage;
    private Stage stage;

    public MainMenu(Parent LoginPage, Stage stage){

        this.prevPage = LoginPage;
        this.stage = stage;


        Label statement = new Label("Sign In");


    }

创建 MainMenu.java 文件后,它不允许我再运行应用程序,并且它发生在第一段代码中。

最佳答案

addViewFactory的签名是

void addViewFactory(java.lang.String viewName, java.util.function.Supplier<View> supplier)

即使用第二个参数的方法引用,您需要使用带有 0 个参数的方法(或构造函数)。 LoginPage 没有构造函数满足此要求;因此编译器不接受LoginPage::new .

至于你的子类的构造函数中的错误View :

View不提供不带参数的构造函数。因此,您需要调用 View 的构造函数之一使用 super(<args>); 显式地在每个构造函数的开头(只要不使用构造函数链接)作为构造函数中的第一条语句。

示例

public MainMenu(Parent LoginPage, Stage stage) {
    super(LoginPage); // use View(javafx.scene.Node content)

    this.prevPage = LoginPage;
    this.stage = stage;

    Label statement = new Label("Sign In");
}
private LoginPage(BorderPane root) {
    super(root);

    //For the label displaying "Sign In" to prompt the user
    VBox login = new VBox();
    Label statement = new Label("Sign In");
    statement.setFont(Font.font("Verdana", FontWeight.BOLD, 13));

    //HBox for the email the user wants to sign in with
    HBox firstUserPrompt = new HBox();
    Label email = new Label("Username/\nEmail: ");
    email.setFont(Font.font("Verdana", 13));
    firstUserPrompt.setAlignment(Pos.CENTER);
    TextField userPrompt = new TextField();

    // TODO: add something to root???
}

public LoginPage() {
    this(new BorderPane());
}

请注意,传递 Stage没有什么意义,因为这是 MobileApplication 的责任处理Scene/Stage创作。

关于java - 如何为移动应用程序设置 init 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56997150/

相关文章:

java - GWT + MySQL - 如何创建连接?

java - 使用字符集编码读取文件

java - 如何在 Java EE Web 应用程序 web.xml 中配置 JMS 监听器?

java - 为什么我会收到 ConcurrentModificationException?

java - Scene Builder 无法打开 FXML 文件

Java Maven 组装插件 编译 fatal error : invalid target release 1. 14

java - 使用 REST Client java 连接到 TFS

java - 无法使用 JavaFX 更改可运行对象中的接口(interface)

java - XWPFDocument 循环替换段落

java - MenuButton 在 Javafx 中不显示项目