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