【问题标题】:How to show make a hidden form visible in javafx如何在javafx中显示使隐藏的表单可见
【发布时间】:2017-03-22 13:56:24
【问题描述】:

我有一个设计为三明治店系统的应用程序,我有一个主菜单表单,它是我的主类,即这个类包含我的主类,并且是执行应用程序时打开的第一个表单。

当我点击创建新的标准订单按钮时​​,我有它,所以它会显示一个可供选择的项目菜单。我在创建新标准订单按钮后面有这段代码,因此它将隐藏第一个窗口并显示下一个窗口。

btnStdOrder.setOnAction(e -> { 
    ((Node)e.getSource()).getScene().getWindow().hide();
    NewOrderPopUp.Display();  
});

但是,我如何返回到第一个窗口?我曾尝试使用与上面相同的代码,但因为第一个表单包含我的 main 和 start 方法,我无法使用上述方法再次调用它们(或者我可以我只是不知道该怎么做)。任何帮助将不胜感激。

【问题讨论】:

  • 如果你 NewOrderPopUp.Display() 以 showandwait() 结尾,你应该使用 ((Node)e.getSource()).getScene().getWindow().show();在 NewOrderPopUp.Display(); 之后
  • @SedrickJefferson 所以我上面的代码位于新标准订单按钮后面,用于显示新订单屏幕(称为 newOrderPopUp)。我想要尝试做的是回到原来的菜单类。例如,我尝试使用此代码btnStdOrder.setOnAction(e -> { ((Node)e.getSource()).getScene().getWindow().hide(); SandwichShopSystem().show(); });,但这不会使第一个菜单在我隐藏后再次显示。
  • 您想何时返回上一个窗口?您要执行什么操作?

标签: java javafx


【解决方案1】:

如果你的 popUpDisplay 方法以 showAndWait() 结束,你可以试试这个:

((Stage)((Node)e.getSource()).getScene().getWindow()).hide();
NewOrderPopUp.Display();  
((Stage)((Node)e.getSource()).getScene().getWindow()).show();

我创建了一个示例应用程序来展示这种行为。

主要

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

/**
 *
 * @author blj0011
 */
public class JavaFXApplication53 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.show();
    }

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

}

控制器 - 主场景

import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.logging.*;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

/**
 *
 * @author blj0011
 */
public class FXMLDocumentController implements Initializable
{    
    @FXML private Button btnMain;  

    Stage window;

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

        // TODO
        btnMain.setOnAction(e -> {
            ((Stage)((Node)e.getSource()).getScene().getWindow()).hide();           
            popUpDisplay();
            ((Stage)((Node)e.getSource()).getScene().getWindow()).show();

        });
    }    

    public void popUpDisplay()
    {
        try
        {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("SceneTwo.fxml"));
            Parent root1 = (Parent) fxmlLoader.load();
            Stage stage = new Stage();
            stage.initModality(Modality.APPLICATION_MODAL);
            //stage.initStyle(StageStyle.UNDECORATED);
            stage.setTitle("PopUp");
            stage.setScene(new Scene(root1));
            stage.showAndWait();
        }
        catch (IOException ex)
        {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

FXML - 主要场景

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane id="AnchorPane" maxHeight="300.0" maxWidth="300.0" minHeight="300.0" minWidth="300.0" prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication53.FXMLDocumentController">
   <children>
      <Button fx:id="btnMain" layoutX="124.0" layoutY="248.0" mnemonicParsing="false" text="Button" />
      <Label layoutX="99.0" layoutY="135.0" text="Main Scene">
         <font>
            <Font size="20.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

控制器 - 弹出窗口

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.*;

/**
 * FXML Controller class
 *
 * @author blj0011
 */
public class SceneTwoController implements Initializable
{
    @FXML Button btnClosePopup;    

    /**
     * Initializes the controller class.
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        // TODO
        btnClosePopup.setOnAction(e -> {
            ((Stage)(((Button)e.getSource()).getScene().getWindow())).close();           
        });
    }    

}

FXML - 弹出窗口

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="javafxapplication53.SceneTwoController">
   <children>
      <Label layoutX="257.0" layoutY="155.0" text="Popup">
         <font>
            <Font size="30.0" />
         </font>
      </Label>
      <Button fx:id="btnClosePopup" layoutX="259.0" layoutY="353.0" mnemonicParsing="false" text="Close Popup" />
   </children>
</AnchorPane>

【讨论】:

  • 效果很好,非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
  • 1970-01-01
  • 2019-05-18
  • 2020-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多