- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我得到了一个 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.fxml
和 MainController
。唯一的区别是按钮会返回主视图。
关于java - 从另一个 Controller 返回 PrimaryStage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56115988/
Yii::$app->runAction('new_controller/new_action', $params); 我相信这可以用来从另一个 Controller 调用 Controller Ac
这个问题类似于 this ,但我需要访问父成员(不是控制)。我不知道是否可以不使用依赖注入(inject)。 例如,我有一个父级,有一个成员调用用户,我需要从子 Controller 访问用户。 最佳
我有包含 2 个布局的根布局:- 选项面板- 绘制区域 我正在尝试的是访问 OptionsPaneController 中的 DrawAreaController 以调用其绘制方法。下面是 Optio
我的应用程序的 View Controller 层次结构设置如下: UIViewController | UITabBarController | UINavigationCo
我的应用程序的 View Controller 层次结构设置如下: UITabBarController | UINavigationController | | |
当我第一次为我目前在 Storyboard 中开发的应用程序创建基础布局时,我分两步完成: 选择我的 View Controller 并使用 Editor->Embed In->Navigation
设计要求: 显示用户可以选择的项目列表 选择一个项目后,使用后退按钮将用户带到一个新 View 。新 View 应在底部包含第一个屏幕中不存在的选项卡列表 单击选项卡中的项目时,应出现一个带有后退按钮
将父 Controller 设置为“parentCtrl as vm”,并将子 Controller 设置为“childCtrl as vmc”,以避免名称冲突,并且效果良好。 如何在子 Contro
我已经阅读了一些答案,例如关闭当前的 ViewController,但我的情况有所不同,因为我正在展示另一个 ViewController。 虽然我无法访问它的属性,但此代码显示了带有导航 Contr
如我所见,如果我们要实例化一个Model(例如,名为Post),我们只需调用: $post = new Post(); 现在,我还想实例化一个Controller(例如,名为Post,并为此 Cont
我已经疯狂地在整个网络上搜索解决我的问题的方法,但目前还没有。我的问题是我必须检查是否在 HTTP 请求中获得特定文本,该请求在一个 while 循环中,如果我这样做了,那么我应该离开循环并继续线程,
我想用this.get('controllers.pack.query');要得到App.PackQueryController在 App.PackController ,但失败了。 我认为问题是 E
我刚开始使用 Laravel。当我使用 codeigniter 或 zend 框架时,我可以将我的 Controller 组织到一个单独的目录中。例如,我可以创建“user/permission.ph
在 emberjs 前 2 我们可以从另一个 Controller 访问 Controller 或 Controller 中的任何方法 以下方式: App.get('router').get('nav
这可能是非常简单的实现,但我是 iOS 编程的新手,我似乎被卡住了。 所以,基本上,我有一个选项卡式应用程序。我决定除了标签栏之外还需要一个导航栏。为此,我放置了标签栏 Controller ,然后添
我有这个列表 Controller , define([ 'jquery', 'app' ], function ($,app) { app.controller("ListC
我有 3 个 Controller :RootController、FirstController 和 SecondController。我想从 RootController -> FirstCont
我有以下 Controller : /controllers/api/base_controller.rb /controllers/api/v1/articles_controller.rb 当为文
我是 Angular JS 的新手,尝试在另一个 Controller 中调用一个 Controller ,但出现以下错误。 ionic.bundle.js:21157 TypeError: $con
我有一个标签栏 Controller 和它的 3 个 child ,我还有另一个 View ,我制作了一个从 child 到 View Controller 的自定义转场,还有一个从 View Con
我是一名优秀的程序员,十分优秀!