英文:
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<ActionEvent>() {
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("Hello World");
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("Switch To Scene 2");
this.getChildren().add(button);
button.setOnAction(event -> sceneLibrary.switchToTwo());
}
}
class PaneTwo extends AnchorPane {
public PaneTwo(SceneLibrary sceneLibrary) {
Button button = new Button("Switch To Scene 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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论