簡體   English   中英

primaryStage 無法解析為變量:,在其他類中使用時:JavaFX

[英]primaryStage cannot be resolved to a variable:,When using in other class: JavaFX

我知道有很多與此特定錯誤相關的問題,但我找不到適合我的錯誤的解決方案。

所以我正在做的是創建一個 javaFX 應用程序並使用Modality庫,以便我可以創建一個始終在主舞台上方打開的子窗口。 但這個錯誤我得到的是我不能夠集內部的primaryStage可變initOwner()函數,因為的Scope問題,因為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 中解決這個問題。 如何在SubModal類中引入primaryStage值,以便我可以運行此代碼 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);
    }
}

我認為你想要的只是在 start 方法中添加代碼。 我不知道 SubModule 類是做什么用的。

進一步的模態不是圖書館。 也許稱之為功能。

也許這個例子有幫助http://java-buddy.blogspot.com/2013/08/javafx-example-modality.html

所以添加

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();

到 start 方法的末尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM