2014-10-28 107 views
0

大家晚上好, 我有一個測試應用程序由兩個窗口組成:Main,Window1。 從主窗口顯示「Window1」窗口,我點擊一個按鈕。我的問題是關閉「window1」窗口會自動關閉應用程序。我希望當我點擊窗口「window1」的(X)時,我必須返回到主窗口(不退出應用程序)。 這裏是代碼:關閉一個窗口,不要退出應用程序

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

}

回答

2

默認情況下,JavaFX應用程序時,所有的窗口都關閉退出。您可以通過在你的Applicationstart(...)方法調用

Platform.setImplicitExit(false); 

阻止這種行爲。

但是,我不確定這是否是您的問題,因爲您的代碼中無處創建第二個窗口:只需加載一個新的FXML文件,將其內容放入新場景中,並將其顯示在現有的窗口:

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

從你的描述,這聽起來像你想創建一個第二個窗口,用戶具有回到原來的窗口前解僱。你可以通過創建一個新的舞臺,使其擁有者現有的舞臺,並使其模式:

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

    // create a new stage: 
    Stage stage = new Stage(); 
    // make it modal: 
    stage.initModality(Modality.APPLICATION_MODAL); 
    // make its owner the existing window: 
    stage.initOwner(existingWindow); 

    stage.setScene(scene); 
    stage.show(); 
} 
+0

我編輯帖子,以澄清問題。我寫了完整的代碼 – Kachna 2014-10-29 09:19:04

+0

儘管在代碼中沒有任何地方創建第二個窗口。我編輯了答案;我仍然不確定你想要做什麼,但也許有幫助。 – 2014-10-29 12:09:59

+0

非常感謝,這正是我想要做的。 – Kachna 2014-10-29 16:11:38

相關問題