关闭窗口,不要退出应用程序

时间:2014-10-28 19:00:02

标签: javafx-8

大家晚上好, 我有一个测试应用程序包含两个窗口:Main,Window1。 从主窗口显示“Window1”窗口我点击一个按钮。我的问题是关闭“window1”窗口会自动关闭应用程序。我希望当我点击窗口“(窗口1)”时,我必须返回主窗口(不退出应用程序)。 这是代码:

MainScreen.fxml

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


<AnchorPane id="AnchorPane" prefHeight="166.0" prefWidth="307.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="testexample.MainScreenController">
   <children>
      <Button layoutX="128.0" layoutY="127.0" mnemonicParsing="false" onAction="#showScreen1" prefHeight="25.0" prefWidth="57.0" text="show..." />
      <Label layoutX="114.0" layoutY="31.0" text="Main Screen">
         <font>
            <Font size="25.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

MainScreenController

public class MainScreenController implements Initializable {

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}

@FXML
private void showScreen1(ActionEvent event) throws IOException {
    Parent root = (Parent) FXMLLoader.load(getClass().getResource("Screen1.fxml"));
    Scene scene = new Scene(root);
    Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    stage.setScene(scene);
    stage.show();
}}

Screen1.fxml

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

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


<AnchorPane id="AnchorPane" prefHeight="202.0" prefWidth="463.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="testexample.Screen1Controller">
    <children>
        <Label layoutX="58.0" layoutY="40.0" prefHeight="53.0" prefWidth="342.0" text="Welcome to screen 1">
            <font>
                <Font size="36.0" />
            </font>
        </Label>
    </children>
</AnchorPane>

包含方法start()和方法main()的类:

public class TestExample extends Application {

@Override
public void start(Stage stage) throws IOException {
    AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
    Scene sc = new Scene(root);
    stage.setScene(sc);
    stage.show();

}

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

}

1 个答案:

答案 0 :(得分:2)

默认情况下,JavaFX应用程序将在所有窗口关闭时退出。您可以通过调用

来防止此行为
Platform.setImplicitExit(false);

在您Application的{​​{1}}方法中。

我不确定这是你的问题,但是,因为你的代码中没有任何地方你创建了第二个窗口:你只需加载一个新的FXML文件,将其内容放在一个新的场景中,并将其显示在现有的窗口:

start(...)

根据您的描述,听起来您想要创建第二个窗口,用户在返回原始窗口之前必须关闭该窗口。您可以通过创建一个新阶段,使其所有者成为现有阶段,并使其成为模态来实现此目的:

@FXML
private void showScreen1(ActionEvent event) throws IOException {

    // Load new FXML file and save root Node as "root":
    Parent root = (Parent) FXMLLoader.load(getClass().getResource("Screen1.fxml"));

    // Create a new Scene to display the root node just loaded:
    Scene scene = new Scene(root);

    // Get a reference to the existing stage (the window containing the source of the event; the "show..." Button)
    Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

    // Set the new scene in the existing stage:
    stage.setScene(scene);

    // Show the existing stage (though it is already showing, I think):
    stage.show();
}