JavaFX使用不同的面板切换场景。

huangapple go评论73阅读模式
英文:

Javafx switching scenes using different panes

问题

以下是翻译好的内容:

public class SceneLibrary {
    private Stage primaryStage;
    private Scene sceneOne = new Scene(new PaneOne(),300,500);
    private Scene sceneTwo = new Scene(new PaneTwo(),400,500);

    public void setPrimaryStage(Stage primaryStage){
        this.primaryStage = primaryStage;
    }

    public void switchToOne(){
        primaryStage.setScene(sceneOne);
    }

    public void switchToTwo(){
        primaryStage.setScene(sceneTwo);
    }
}

我的 PaneOne 类:

public class PaneOne extends AnchorPane {
    private SceneLibrary sceneLibrary = new SceneLibrary();
    public PaneOne() {
        Button button = new Button();
        this.getChildren().add(button);

        button.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                sceneLibrary.switchToTwo();
            }
        });
    }
}

我的 Main 类:

public class Main extends Application {
    private SceneLibrary sceneLibrary = new SceneLibrary();
    @Override
    public void start(Stage primaryStage) throws Exception{
        AnchorPane root = new PaneOne();
        primaryStage.setTitle("Hello World");
        sceneLibrary.setPrimaryStage(primaryStage);
        primaryStage.setScene(new Scene(root, 300, 500));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
英文:

I have class SceneLibrary which I would like to use to switch scenes. When I run application I have < init > errors at PaneOne private SceneLibrary sceneLibrary = new SceneLibrary(); and < init > errors at SceneLirary private Scene sceneOne = new Scene(new PaneOne(),300,500);. I can not use static scenes.

public class SceneLibrary {
    private Stage primaryStage;
    private Scene sceneOne = new Scene(new PaneOne(),300,500);
    private Scene sceneTwo = new Scene(new PaneTwo(),400,500);

    public void setPrimaryStage(Stage primaryStage){
        this.primaryStage = primaryStage;
    }

    public void switchToOne(){
        primaryStage.setScene(sceneOne);
    }

    public void switchToTwo(){
        primaryStage.setScene(sceneTwo);
    }
}

My PaneOne class:

public class PaneOne extends AnchorPane {
    private SceneLibrary sceneLibrary = new SceneLibrary();
    public PaneOne() {
        Button button = new Button();
        this.getChildren().add(button);

        button.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
            public void handle(ActionEvent event) {
                sceneLibrary.switchToTwo();
            }
        });
    }
}

My Main class:

public class Main extends Application {
    private SceneLibrary sceneLibrary = new SceneLibrary();
    @Override
    public void start(Stage primaryStage) throws Exception{
        AnchorPane root = new PaneOne();
        primaryStage.setTitle(&quot;Hello World&quot;);
        sceneLibrary.setPrimaryStage(primaryStage);
        primaryStage.setScene(new Scene(root, 300, 500));
        primaryStage.show();
    }

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

答案1

得分: 1

分享一个 `sceneLibrary` 的实例

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        SceneLibrary sceneLibrary = new SceneLibrary();
        sceneLibrary.setPrimaryStage(primaryStage);
        sceneLibrary.switchToOne(); // 将场景一设置到舞台
        primaryStage.show();
    }

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

class PaneOne extends AnchorPane {

    public PaneOne(SceneLibrary sceneLibrary) {
        Button button = new Button("切换到场景 2");
        this.getChildren().add(button);
        button.setOnAction(event -> sceneLibrary.switchToTwo());
    }
}

class PaneTwo extends AnchorPane {

    public PaneTwo(SceneLibrary sceneLibrary) {
        Button button = new Button("切换到场景 1");
        this.getChildren().add(button);
        button.setOnAction(event -> sceneLibrary.switchToOne());
    }
}

class SceneLibrary {

    private Stage primaryStage;
    private final Scene sceneOne = new Scene(new PaneOne(this), 300, 500);
    private final Scene sceneTwo = new Scene(new PaneTwo(this), 400, 500);

    public void setPrimaryStage(Stage primaryStage){
        this.primaryStage = primaryStage;
    }

    public void switchToOne(){
        primaryStage.setScene(sceneOne);
    }

    public void switchToTwo(){
        primaryStage.setScene(sceneTwo);
    }
}
英文:

Share an instance of sceneLibrary:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
SceneLibrary sceneLibrary = new SceneLibrary();
sceneLibrary.setPrimaryStage(primaryStage);
sceneLibrary.switchToOne();//set scene one to the stage 
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class PaneOne extends AnchorPane {
public PaneOne(SceneLibrary sceneLibrary) {
Button button = new Button(&quot;Switch To Scene 2&quot;);
this.getChildren().add(button);
button.setOnAction(event -&gt; sceneLibrary.switchToTwo());
}
}
class PaneTwo extends AnchorPane {
public PaneTwo(SceneLibrary sceneLibrary) {
Button button = new Button(&quot;Switch To Scene 1&quot;);
this.getChildren().add(button);
button.setOnAction(event -&gt; sceneLibrary.switchToOne());
}
}
class SceneLibrary {
private Stage primaryStage;
private final Scene sceneOne = new Scene(new PaneOne(this),300,500);
private final Scene sceneTwo = new Scene(new PaneTwo(this),400,500);
public void setPrimaryStage(Stage primaryStage){
this.primaryStage = primaryStage;
}
public void switchToOne(){
primaryStage.setScene(sceneOne);
}
public void switchToTwo(){
primaryStage.setScene(sceneTwo);
}
}

huangapple
  • 本文由 发表于 2020年5月2日 15:07:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61555662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定