gpt4 book ai didi

javafx如何在辅助窗口关闭时保留数据

转载 作者:行者123 更新时间:2023-12-01 09:11:51 30 4
gpt4 key购买 nike

问题很简单,我创建了一个小程序,有两个阶段,主窗口和辅助阶段,主窗口有一个按钮来启动第二阶段,当我在窗口中输入文本时,第二阶段有文本字段、按钮和标签文本字段并按下按钮,文本将显示在标签中,我想要做的是,当我关闭辅助阶段并且程序仍在运行时,我希望数据相同,因此当按下按钮并再次启动辅助阶段时我将看到最后的结果,就好像我从未关闭第二阶段一样

这是主类

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {
private Stage primaryStage;
@Override
public void start(Stage primaryStage) throws Exception{
this.primaryStage=primaryStage;
mainWindow();
}

void mainWindow() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("FirstWindow.fxml"));
Parent root = loader.load();
Controller controller = loader.getController();
controller.setMain(this ,primaryStage);
Scene scene = new Scene(root);

primaryStage.setScene(scene);
primaryStage.show();

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

}

public void secondaryWindow() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("SecondWindow.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);

Stage secondaryStage=new Stage();
secondaryStage.initOwner(primaryStage);
secondaryStage.initModality(Modality.WINDOW_MODAL);
swController controller = loader.getController();
controller.setMain(this,secondaryStage);

secondaryStage.setScene(scene);
secondaryStage.show();

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

}


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



}

以及第二阶段控制类

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ResourceBundle;

/**
* Created by ahmednageeb on 11/29/16.
*/
public class swController implements Initializable {
private Main main;
private Stage secondaryStage;

public void setMain(Main main, Stage secondaryStage) {
this.main = main;
this.secondaryStage = secondaryStage;}
@FXML
private Label label;
@FXML
private TextField text;



@FXML
private void change(ActionEvent event) {
String a=text.getText();
label.setText(a);
text.clear();

}
public void goBack() {
secondaryStage.close();
}
@Override
public void initialize(URL location, ResourceBundle resources) {

}
}

最后是第一级 Controller

package sample;

import javafx.stage.Stage;

public class Controller {
private Main main;
private Stage primaryStage;


public void setMain(Main main,Stage primaryStage){
this.main=main;
this.primaryStage =primaryStage;
}

public void close(){
primaryStage.close();
}



public void changeWindow(){
main.secondaryWindow();

}
}

最佳答案

一种解决方案可能是在启动时加载辅助窗口但隐藏它。然后使用按钮来显示它。当您关闭它时,再次隐藏它。

看看这样的东西是否有效!

Main:

package hidepopup;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class HidePopup extends Application {

@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("first stage");
stage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

First FXML:

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="hidepopup.FXMLDocumentController">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
</children>
</AnchorPane>

First Controller:

package hidepopup;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class FXMLDocumentController implements Initializable {

@FXML
private Label label;

FXMLLoader loader;
Parent root2;
Stage stage2;

@FXML
private void handleButtonAction(ActionEvent event) {
try
{
Scene scene2 = new Scene(root2);
stage2.setScene(scene2);
stage2.setTitle("second stage");
stage2.showAndWait();
}
catch(IllegalArgumentException ex)
{
stage2.show();
}
}

@Override
public void initialize(URL url, ResourceBundle rb) {
try
{
loader = new FXMLLoader(getClass().getResource("FXMLPopup.fxml"));
root2 = loader.load();
FXMLPopupController dac = (FXMLPopupController) loader.getController();
stage2 = new Stage();
}
catch (IOException ex)
{
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

Second FXML:

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="hidepopup.FXMLPopupController">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
</children>
</AnchorPane>

Second Controller

package hidepopup;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class FXMLPopupController implements Initializable {

@FXML
public Label label;

@FXML
private void handleButtonAction(ActionEvent event){
label.getScene().getWindow().hide();
}

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}

两个窗口的布局完全相同,因此按下按钮后,您可能希望将顶部布局滑过去。另外,我没有测试第二阶段是否保存其数据。在第二阶段添加一个文本框并进行测试。我希望这会有所帮助。

关于javafx如何在辅助窗口关闭时保留数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40872596/

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