gpt4 book ai didi

java - 从另一个 Controller 返回 PrimaryStage

转载 作者:行者123 更新时间:2023-12-02 05:43:42 25 4
gpt4 key购买 nike

我得到了一个 Mainpage.fxml,并且在同一阶段打开了一个 Objectpage.fxml。现在我需要通过 Labelpress 返回 Mainpage.fxml,但我无法让它工作。

我已经尝试在我的 BusinessLogic 中调用一个方法,但它需要是静态的。我不能允许它是静态的,因为其他代码部分不起作用。

业务逻辑.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package softwareprojekt;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONException;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import softwareprojekt.model.Event;
import softwareprojekt.model.MqttEvent;
import softwareprojekt.model.MqttObject;
import softwareprojekt.model.Subject;
import softwareprojekt.util.Parser;
import softwareprojekt.view.EventpageController;
import softwareprojekt.view.MainpageController;
import softwareprojekt.view.NewObjectDialogController;
import softwareprojekt.view.ObjectpageController;

public class BusinessLogic extends Application {

private Stage primaryStage;
private BorderPane rootLayout;
private static final ObservableList<Event> eventData = FXCollections.observableArrayList();
private static final ObservableList<Subject> subjectData = FXCollections.observableArrayList();
private static ArrayList<MqttObject> objects = new ArrayList<MqttObject>();
private static ArrayList<MqttEvent> events = new ArrayList<MqttEvent>();

public BusinessLogic() {
eventData.add(new Event("Olaf", "Olafs Zeitpunkt"));
subjectData.add(new Subject("Peter", "Peters key", "Peters devID", "Peters appID"));
}

public static ObservableList<Event> getEventData() {
return eventData;
}

public static ObservableList<Subject> getSubjectData() {
return subjectData;
}

@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("OneTimeNotifier");

initRootLayout();

showMainpage();
}

public void init() throws FileNotFoundException, IOException, Exception {
objects = KeyLoader.loadObjects("keyholder.txt");
for (MqttObject obj : objects) {
ClientMQTT mqttObj = new ClientMQTT();
obj.setMqttObj(mqttObj);
mqttObj.setAPP_ID(obj.getAppId());
mqttObj.setKey(obj.getKey());
mqttObj.start();
}
}

public void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
// Load root layout from fxml file.
loader.setLocation(BusinessLogic.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();

// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);

// Give the controller access to the main app.
// RootLayoutController controller = loader.getController();
// controller.setBusinessLogic(this);

primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}

public void showMainpage() {
try {
FXMLLoader loader = new FXMLLoader();
// Load maintenance overview.
loader.setLocation(BusinessLogic.class.getResource("view/Mainpage.fxml"));
AnchorPane mainpage = (AnchorPane) loader.load();

// Set maintenance overview into the center of root layout.
rootLayout.setCenter(mainpage);

// Give the controller access to the main app.
MainpageController controller = loader.getController();
controller.setBusinessLogic(this);

} catch (IOException e) {
e.printStackTrace();
}

}

public void showEventpage() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(BusinessLogic.class.getResource("view/Eventpage.fxml"));
AnchorPane page = (AnchorPane) loader.load();
primaryStage.setTitle("Eventpage");
Scene scene = new Scene(page);
primaryStage.setScene(scene);

// Give the controller access to the main app.
EventpageController controller = loader.getController();
controller.setBusinessLogic(this);

primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}

public void backToMainpage() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(BusinessLogic.class.getResource("view/Mainpage.fxml"));
AnchorPane page = (AnchorPane) loader.load();
primaryStage.setTitle("Mainpage");
Scene scene = new Scene(page);
primaryStage.setScene(scene);

// Give the controller access to the main app.
ObjectpageController controller = loader.getController();
controller.setBusinessLogic(this);

primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}

public void showObjectpage() throws IOException {

try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(BusinessLogic.class.getResource("view/Objectpage.fxml"));
AnchorPane page = (AnchorPane) loader.load();
primaryStage.setTitle("Objectpage");
Scene scene = new Scene(page);
primaryStage.setScene(scene);

// Give the controller access to the main app.
ObjectpageController controller = loader.getController();
controller.setBusinessLogic(this);

primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}

public boolean showSubjectEditDialog(Subject subject) {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(BusinessLogic.class.getResource("view/NewObjectDialog.fxml"));
AnchorPane page = (AnchorPane) loader.load();

Stage dialogStage = new Stage();
dialogStage.setTitle("Edit subject");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);

NewObjectDialogController controller = loader.getController();
controller.setDialogStage(dialogStage);
controller.setSubject(subject);

dialogStage.showAndWait();

return controller.isOkClicked();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

public Stage getPrimaryStage() {
return primaryStage;
}

public static void messageArrived(String jsonResponse) throws JSONException {
MqttEvent newEvent = Parser.parseJSON(jsonResponse, null, "status");
if (newEvent != null) {
events.add(newEvent);
// updateGUI();
}
}

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

public static ObservableList<MqttEvent> getEventsGUI() {
ObservableList<MqttEvent> currList = FXCollections.observableArrayList(events);
return currList;
}

public static ObservableList<MqttObject> getObjectsGUI() {
ObservableList<MqttObject> currList = FXCollections.observableArrayList(objects);
return currList;
}
}

对象页面 Controller :

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package softwareprojekt.view;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
import softwareprojekt.BusinessLogic;
import softwareprojekt.model.Subject;

/**
*
* @author Schurke
*/
public class ObjectpageController {



@FXML
private Label labelTitle;
@FXML
private Label labelText1;
@FXML
private Label labelText2;
@FXML
private Label labelText3;
@FXML
private Label labelText4;
@FXML
private Label labelBack;
@FXML
private Button buttonSub;
@FXML
private Button buttonUnsub;
@FXML
private Button buttonRefresh;
@FXML
private TableView<Subject> subjectTable;
@FXML
private TableColumn<Subject, String> column1;
@FXML
private TableColumn<Subject, String> column2;

private BusinessLogic businessLogic;

@FXML
private void initialize(){
// Initialize the object table with the two columns.
column1.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
column2.setCellValueFactory(cellData -> cellData.getValue().keyProperty());

// Listen for selection changes and show the object details when changed.
subjectTable.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> showSubjectDetails(newValue));
}

public void setBusinessLogic(BusinessLogic businessLogic){
this.businessLogic = businessLogic;

subjectTable.setItems(BusinessLogic.getSubjectData());
}

@FXML
private void handleButtonSub(){
Subject tempSubject = new Subject();
boolean okClicked = businessLogic.showSubjectEditDialog(tempSubject);
if (okClicked){
BusinessLogic.getSubjectData().add(tempSubject);
}

}
@FXML
private void handleButtonEdit(){
Subject selectedSubject = subjectTable.getSelectionModel().getSelectedItem();
if (selectedSubject != null){
boolean okClicked = businessLogic.showSubjectEditDialog(selectedSubject);
if (okClicked){
showSubjectDetails(selectedSubject);
}
} else {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.initOwner(businessLogic.getPrimaryStage());
alert.setTitle("No Selection");
alert.setHeaderText("No Subject selected");
alert.setContentText("Please select a subject.");

alert.showAndWait();
}
}

@FXML
private void handleButtonUnsub() {
int selectedIndex = subjectTable.getSelectionModel().getSelectedIndex();
if(selectedIndex >= 0){
subjectTable.getItems().remove(selectedIndex);
} else {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.initOwner(businessLogic.getPrimaryStage());
alert.setTitle("No Selection");
alert.setHeaderText("No Subject selected");
alert.setContentText("Please select a subject.");

alert.showAndWait();
}
}
@FXML
private void handleButtonRefresh() {

}
@FXML
private void backActionHandler(){
Stage stage = (Stage) labelBack.getScene().getWindow();
stage.close();


}

private void showSubjectDetails(Subject subject) {
if (subject != null) {
labelText1.setText(subject.getName());
labelText2.setText(subject.getKey());
labelText3.setText(subject.getDevID());
labelText4.setText(subject.getAppID());
} else {
labelText1.setText("");
labelText2.setText("");
labelText3.setText("");
labelText4.setText("");
}
}
}

我刚刚回到关闭窗口。但是 backActionHandler 应该打开 Mainpage.fxml

最佳答案

您是否尝试过使用 StackPane 来代替?只需设置一个包含面板的 StackPane 并显示/隐藏或将正确的面板置于后面/前面即可。无需多次重新加载相同的 FXML。

额外的好处:切换到对象页面并返回后,主窗口状态不会丢失。

这里是如何使用 StackPane 切换 UI 组件的示例,使用多个 FXML 并包含。

top.fxml - 定义堆栈 Pane 并包括实际 View :

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.StackPane?>

<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="de.reinhardt.playground.javafx.TopController">
<fx:include fx:id="main" source="main.fxml"/>
<fx:include fx:id="object" source="object.fxml" />
</StackPane>

TopController.java - 使用导入的 Controller 在 View 之间切换的实现

public class TopController implements Initializable {
@FXML
private MainController mainController;
@FXML
private ObjectController objectController;

@Override
public void initialize(URL location, ResourceBundle resources) {
mainController.setTopController(this);
objectController.setTopController(this);
showMainPage();
}

public void showMainPage() {
objectController.hidePage();
mainController.showPage();
}

public void showObjectPage() {
mainController.hidePage();
objectController.showPage();
}
}

main.fxml - 主视图的实现,包括一个转到对象 View 的按钮

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

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<VBox xmlns:fx="http://javafx.com/fxml" fx:id="mainPage" fx:controller="de.reinhardt.playground.javafx.MainController">
<Label>Your Main Components go here.</Label>
<Button onAction="#toObjectPage">To Object Page</Button>
</VBox>

MainController -- 主视图的 Controller

public class MainController {
private TopController topController;
@FXML
private Node mainPage;

public void showPage() {
mainPage.setVisible(true);
mainPage.toFront();
}

public void hidePage() {
mainPage.setVisible(false);
}

public void setTopController(TopController topController) {
this.topController = topController;
}

@FXML
public void toObjectPage() {
topController.showObjectPage();
}
}

object.fxml 定义对象 View ,并省略相应的ObjectController。它们类似于 main.fxmlMainController。唯一的区别是按钮会返回主视图。

关于java - 从另一个 Controller 返回 PrimaryStage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56115988/

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