0
A
回答
0
如果我的应用程序应该在一个窗口中工作,我更喜欢使用GUI管理器singleton类,它管理更改窗口。下面我提供了使用这种机制的简单应用程序的完整代码。假设所有文件都在一个包中,称为sample
。
Main.java - 在此初始化JavaFX组件:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/sample/root.fxml"));
try{
StackPane rootPane;
rootPane = loader.load();
GuiManager guiModel = GuiManager.getInstance();
guiModel.setRootPane(rootPane);
Scene scene = new Scene(rootPane);
primaryStage.setScene(scene);
primaryStage.show();
guiModel.changeWindow("/sample/firstwindow.fxml");
} catch (IOException exception) {
exception.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
root.fxml - 所有的窗户都应该是基于它:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.StackPane?>
<StackPane fx:id="rootPane"
xmlns="http://javafx.com/javafx/8.0.60"
xmlns:fx="http://javafx.com/fxml/1"
prefWidth="1" prefHeight="1"/>
firstwindow.fxml - 第一个实际将显示的窗口:
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="sample.FirstWindowController">
<Label text="First window"/>
<Button text="Change window" onAction="#changeWindow"/>
</VBox>
FirstWindowController.java - 控制器类第一窗口:
package sample;
import javafx.fxml.FXML;
public class FirstWindowController {
@FXML
private void changeWindow() {
GuiManager.getInstance().changeWindow("/sample/secondwindow.fxml");
}
}
secondwindow.fxml - 它将被显示点击第一窗口的按钮后:
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="sample.SecondWindowController" >
<Label text="Second window"/>
<Button text="Change window" onAction="#changeWindow"/>
</VBox>
SecondWindowController.java - 控制器类第二窗口的:
package sample;
import javafx.fxml.FXML;
public class SecondWindowController {
@FXML
private void changeWindow() {
GuiManager.getInstance().changeWindow("/sample/firstwindow.fxml");
}
}
GuiManager.java - 根据根管理更改窗口的类:
package sample;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.Window;
import java.io.IOException;
public class GuiManager {
private StackPane rootPane;
private static GuiManager instance;
public static GuiManager getInstance() {
if (instance == null) {
instance = new GuiManager();
}
return instance;
}
private GuiManager() {}
public void changeWindow(String path) {
changeWindow(rootPane, path, this);
rootPane.setPrefWidth(-1);
rootPane.setPrefHeight(-1);
}
public static void changeWindow(Pane pane, String newWindowPath, Object callingController) {
Window window = pane.getScene().getWindow();
double x = window.getX() + getHorizontalMidpoint(window);
double y = window.getY() + getVerticalMidpoint(window);
ObservableList<Node> childrenList = pane.getChildren();
removeAllIncludedChildren(childrenList);
FXMLLoader loader = new FXMLLoader(callingController.getClass().getResource(newWindowPath));
try {
pane.getChildren().add(loader.load());
Stage primaryStage = (Stage) window;
primaryStage.setMinHeight(0);
primaryStage.setMinWidth(0);
window.sizeToScene();
window.setX(x - getHorizontalMidpoint(window));
window.setY(y - getVerticalMidpoint(window));
primaryStage.setMinHeight(window.getHeight());
primaryStage.setMinWidth(window.getWidth());
} catch (IOException exception) {
exception.printStackTrace();
}
}
private static double getHorizontalMidpoint(Window window) {
int horizontalBisectionCoefficient = 2;
return window.getWidth()/horizontalBisectionCoefficient;
}
private static double getVerticalMidpoint(Window window) {
int verticalBisectionCoefficient = 2;
return window.getHeight()/verticalBisectionCoefficient;
}
private static void removeAllIncludedChildren(ObservableList<Node> childrenList) {
for (int childIndex = 0; childIndex < childrenList.size(); childIndex++) {
childrenList.remove(childIndex);
}
}
public void setRootPane(StackPane rootPane) {
this.rootPane = rootPane;
}
}
0
我只是在同一个问题上运行,并且this answer完美解决了我的问题,同时又短又干净。
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
//Here I want to swap the screen!
Stage stageTheEventSourceNodeBelongs = (Stage) ((Node)event.getSource()).getScene().getWindow();
// OR
Stage stageTheLabelBelongs = (Stage) label.getScene().getWindow();
// these two of them return the same stage
// Swap screen
stage.setScene(new Scene(new Pane()));
}
PS .:记住要点击原来的答案,并给予好评的。这家伙值得...
PPS .:我不知道只是复制一个答案是好的(而不是通过评论分享链接),但由于这没有一个正确的答案,但我决定做它的可见性。
相关问题
- 1. JavaFX传递舞台/切换场景
- 2. 如何在javafx中更新舞台
- 3. JavaFX如何更改舞台
- 4. 在javaFX中摇晃舞台
- 5. Javafx场景切换导致舞台大小增加
- 6. JavaFX舞台背景颜色?
- 7. 打印舞台与JavaFX 8
- 8. JavaFX辅助舞台onCloseRequest
- 9. JavaFX的传球和舞台
- 10. 如何在javaFX中设置内部舞台
- 11. 如何在JavaFx 2.0中向舞台添加其他场景
- 12. JavaFX中的隐形舞台场景
- 13. 在JavaFX舞台上设置图标
- 14. 如何在JavaFX中切换场景
- 15. 如何从JavaFX中的UI控件中检索舞台
- 16. 如何在javafx2中将舞台与舞台绑定
- 17. 如何动态更改JavaFx中舞台的高度
- 18. 如何在javaFX的舞台右侧固定滚动条
- 19. 如何在一段时间后关闭舞台JavaFX
- 20. 在javaFX中切换场景
- 21. JavaFX - 每个舞台的独特场景
- 22. JavaFX我可以重绘舞台吗?
- 23. javafx - 警报和舞台焦点
- 24. JavaFX 8:舞台镶嵌(窗饰厚度)?
- 25. JavaFX:舞台关闭处理程序
- 26. Javafx舞台可见性属性
- 27. JavaFX:我如何为我的舞台设置背景图片
- 28. 如何从JavaFX应用程序类别检索舞台
- 29. JavaFx:我如何将屏幕分辨率与舞台绑定?
- 30. 如何使JavaFX的舞台透明(第一阶段只)
'stage.hide()'将关闭一个阶段。创建一个新的舞台并调用show()来显示它。 –
https://github.com/sedj601/SimpleLoginFx – Sedrick