一、效果:下面两个界面,点击“转到...”就可以相互切换
二、项目目录:
三、代码:
MainApp:
package cn.wyj;
import javafx.application.Application;
import javafx.stage.Stage;
public class MainApp extends Application {
public void start(Stage primaryStage) throws Exception {
StageController stageController = new StageController();
stageController.setStage(primaryStage);
stageController.initScene1();
stageController.initScene2();
stageController.show_scene1();
}
}
StageController:
package cn.wyj;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import java.io.IOException;
public class StageController {
static Parent root1 =null;
static Parent root2 =null;
static Scene scene1=null;
static Scene scene2=null;
public void setStage(Stage stage) {
this.stage = stage;
}
private static Stage stage=null;
public boolean initScene1() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/fxml/f1.fxml"));
root1 = loader.load();
return true;
}
public boolean initScene2() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/fxml/f2.fxml"));
root2 = loader.load();
return true;
}
public boolean show_scene1() throws IOException {
if(stage==null||root1==null) {
return false;
}
if(scene1==null)
{
scene1 = new Scene(root1);
}
stage.setScene(scene1);
stage.show();
return true;
}
public boolean show_scene2() throws IOException {
if(stage==null||root2==null) {
return false;
}
if(scene2==null)
{
scene2 = new Scene(root2);
}
stage.setScene(scene2);
stage.show();
return true;
}
@FXML
void onView1Button1_click(MouseEvent event) throws IOException {
show_scene2();
}
@FXML
void view2Button1_onAction(ActionEvent event) throws IOException {
show_scene1();
}
}
四、fxml文件:
f1.xml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.wyj.StageController">
<children>
<Button fx:id="view1Button1" layoutX="235.0" layoutY="122.0" mnemonicParsing="false" onMouseClicked="#onView1Button1_click" prefHeight="23.0" prefWidth="107.0" text="转到场景2" textFill="#0b52b4" />
<Label layoutX="265.0" layoutY="68.0" prefHeight="24.0" prefWidth="84.0" text="场景1">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</AnchorPane>
f2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.wyj.StageController">
<children>
<Button fx:id="view2Button1" layoutX="243.0" layoutY="149.0" mnemonicParsing="false" onAction="#view2Button1_onAction" text="转到场景1" />
<Label layoutX="234.0" layoutY="77.0" text="这里是场景2" textFill="#2529e5">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</AnchorPane>