我知道有很多与此特定错误相关的问题,但是我找不到适合我的错误的解决方案。

因此,我要做的是创建一个javaFX Application,并使用Modality库,以便我可以创建一个始终在Primary Stage上方打开的子窗口。但是出现的错误是由于initOwner()问题,我无法在Scope函数内设置primaryStage变量,因为PrimaryStage不在SubModal类的范围内。

让我输入一些代码以使事情变得清楚。

//SubModal Class
class SubModal extends singleModal {

    SubModal()
    {
        Stage subStage1 = new Stage();
        subStage1.setTitle("New Stage1");
        FlowPane root = new FlowPane();
        root.setAlignment(Pos.CENTER);
        Scene scene1 = new Scene(root, 300, 200);
        Button btn2 = new Button("Button: Stage1");
        root.getChildren().add(btn2);
        btn2.setOnAction(eve-> System.out.println("Clicked on Stage 1 Button"));
        subStage1.initOwner(primaryStage);
        subStage1.initModality(Modality.NONE);
        subStage1.setScene(scene1);
        subStage1.show();
    }
}

//SingleModal Class
public class singleModal extends Application {

    public static void main(String[] args) {

        Application.launch(args);
    }

    public void start(Stage primaryStage) {

        primaryStage.setTitle("PrimaryStage");

        FlowPane root = new FlowPane();
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 700, 200);
        Button btn = new Button("Open New Stage");
        btn.setOnAction(eve-> new NewStage());
        root.getChildren().add(btn);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}


从上面的代码..

subStage1.initOwner(primaryStage);

此特定行显示错误


  primaryStage无法解析为变量


我知道这是因为PrimaryStage()在subModal类中不可用。

所以我的问题是,如何在JavaFX中解决此问题。如何将primaryStage值带入SubModal类,以便可以运行此Code ErrorFree

最佳答案

对于所需的功能(“创建始终在主要阶段上方打开的子窗口”),无需扩展singleModal
这是一个mre演示:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class SingleModal extends Application {

    @Override
    public void start(Stage primaryStage)  {

        Stage subStage1 = new Stage();
        subStage1.setTitle("New Stage1");

        Button btn2 = new Button("Button: Stage1");
        FlowPane root2 = new FlowPane();
        root2.setAlignment(Pos.CENTER);
        root2.getChildren().add(btn2);
        btn2.setOnAction(eve-> System.out.println("Clicked on Stage 1 Button"));
        subStage1.initOwner(primaryStage);
        subStage1.initModality(Modality.NONE);
        Scene scene1 = new Scene(root2, 300, 200);
        subStage1.setScene(scene1);

        primaryStage.setTitle("PrimaryStage");
        FlowPane root = new FlowPane();
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 700, 200);
        Button btn = new Button("Open New Stage");
        btn.setOnAction(eve->  subStage1.show());
        root.getChildren().add(btn);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

关于java - primaryStage无法解析为变量:,在其他类中使用时:JavaFX,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59478733/

10-10 08:56