gpt4 book ai didi

java - 如何在场景生成器的舞台上显示不同的 fxml 文件?切换场景?

转载 作者:行者123 更新时间:2023-12-02 03:15:03 24 4
gpt4 key购买 nike

还有其他关于此类的教程,但它们并不是非常基本方式。他们中的大多数都有其他方面,这使得像我这样的初学者更难理解......

如何使用按钮切换舞台上的场景或 FXML 文件?

Main.Java 类

package application;


import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

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

@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Screen 1.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}


}

Controller.java类

package application;

import javafx.fxml.FXMLLoader;

public class Controller {

public void goToScreen2(){
System.out.println("Going to screen2");



}

public void goToScreen1(){
System.out.println("Going to screen1");





}
}

屏幕 1 fxml 文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<Button layoutX="203.0" layoutY="132.0" mnemonicParsing="false" onAction="#goToScreen2" text="Go to screen 2" />
</children>
</Pane>

屏幕 2 fxml 文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<Button layoutX="248.0" layoutY="116.0" mnemonicParsing="false" onAction="#goToScreen1" text="Go to screen 1" />
</children>
</Pane>

最佳答案

将 Controller 存储在应用程序中,并根据需要显示关联的组件。

Main.Java 类

public class Main extends Application {
public Controller controller1;
public Controller controller2;
private Stage primaryStage;

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

@Override
public void start(final Stage primaryStage) throws IOException {
this.primaryStage = primaryStage;

controller1 = load("Screen 1.fxml");
controller2 = load("Screen 2.fxml");

primaryStage.setTitle("Hello World");
controller1.goToScreen1();
primaryStage.show();
}

public Controller load(final String name) throws IOException {
final FXMLLoader loader = new FXMLLoader(getClass().getResource(name));
loader.load();
final Controller controller = loader.getController();
controller.configure(this, new Scene(loader.getRoot()));
return controller;
}

public void show(final Scene scene) {
primaryStage.setScene(scene);
}
}

Controller.java类

public class Controller {
private Main application;
private Scene scene;

public void configure(final Main application, final Scene scene) {
this.application = application;
this.scene = scene;
}

public void show() {
application.show(scene);
}

public void goToScreen2() {
application.controller2.show();
}

public void goToScreen1() {
application.controller1.show();
}
}

关于java - 如何在场景生成器的舞台上显示不同的 fxml 文件?切换场景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40453739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com