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

时间:2016-11-29 17:57:24

标签: java database javafx

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

这是主要的课程

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) {

    }
}

最后是第一阶段控制器

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();

    }
}

1 个答案:

答案 0 :(得分:1)

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

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

  

主:

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>
  

第一控制器:

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);
        }
    } 
}
  

第二个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>
  

第二控制器

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
    }     
}

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