JavaFX新的自定义弹出窗口 [英] JavaFX new custom pop out window

查看:1673
本文介绍了JavaFX新的自定义弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索JavaFX中弹出窗口的示例。
我有JavaFX应用程序,有一次我需要弹出一个窗口来显示。此弹出窗口需要一些复杂的输入,我需要对其进行处理和检查,然后返回到主应用程序/窗口。

I am searching for the example of pop out windows in JavaFX. I have JavaFX application and at one point I need a pop out window to appear. this popout windows needs some complicated inputs which I need to process and check and come back to main appliation/window.

现在的问题是我在任何地方都找不到示例,现在如何在一个JavaFX控制器类中调用JavaFX弹出窗口?我只找到样例如何使Dialog弹出窗口,但找不到基于JavaFX的新弹出窗口的示例(我看到一个解决方案,其中并行存在两个窗口,但仅在需要时才需要创建一个窗口) 。

Now the problem is I can not find anywhere an example how in one JavaFX controller class call now JavaFX pop out window? I only find examle how to make Dialog pop out windows but I can not find example of a new pop out window based on JavaFX (I saw one solution where there is two windows in paralel but I would need one to be created only when needed).

您知道这样的JavaFx自定义弹出窗口示例吗?

Do you know of such example of JavaFx custom popout window?

推荐答案

我想我理解您想要的,这是一个(解决方法)示例:

I think I understand what you want, here is an (workaround) example:


  • 我已经创建了两个FXML文件,一个对于主窗口(MainWindow.fxml)和一个弹出窗口(Popup.fxml)

  • 为每个fxml文件创建了两个控制器类,这些控制器扩展了AbstractContoller类,这很有趣进入这两个控制器。

  • 抽象控制器类只有一种方法,允许具体控制器访问主应用程序

  • mainApp类,只需加载MainWindow的控制器并将MainWindow设置为

  • I've created two FXML files, one for the main window(MainWindow.fxml) and one for popup window (Popup.fxml)
  • Created two controller classes for each fxml file, these controllers extend an AbstractContoller class, all the interesting thing goes into those two controllers.
  • The abstract controller class has just one method that allows concrete controllers to have access to the main application
  • Nothing fancy in the mainApp class, just loading the controller of the MainWindow and setting MainWindow as the root of the primary stage scene.

github上的源代码

屏幕截图

MainWindow.fxml

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


<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label text="I'm the MAiN here">
         <font>
            <Font size="24.0" />
         </font>
      </Label>
      <Label text="LETS POP THIS OUT">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <Button fx:id="popitBtn" mnemonicParsing="false" text="NOW">
         <font>
            <Font size="14.0" />
         </font>
      </Button>
      <Label fx:id="resultLbl" text="I've got this (username: /Password: )">
         <VBox.margin>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </VBox.margin>
      </Label>
   </children>
   <padding>
      <Insets bottom="40.0" left="40.0" right="40.0" top="40.0" />
   </padding>
</VBox>

Popup.fxml

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

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


<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <HBox alignment="CENTER" style="-fx-background-color: #e1c1c1;">
         <children>
            <Label text="POPUP WINDOW EXAMPLE" textFill="#752b2b">
               <font>
                  <Font size="14.0" />
               </font>
            </Label>
         </children>
      </HBox>
      <HBox alignment="CENTER">
         <children>
            <Label prefWidth="70.0" text="Username" />
            <TextField fx:id="usernameTF" promptText="John Doe" />
         </children>
      </HBox>
      <HBox alignment="CENTER">
         <children>
            <Label prefWidth="70.0" text="Password" />
            <PasswordField fx:id="passwordPF" promptText="*********" />
         </children>
      </HBox>
      <HBox alignment="CENTER">
         <children>
            <Button fx:id="connectBtn" mnemonicParsing="false" text="Connect" />
         </children>
      </HBox>
   </children>
   <padding>
      <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
   </padding>
</VBox>

AbstractController.java

public abstract class AbstractController {

    protected MainApp main;

    public void setMainApp(MainApp main) {
        this.main = main;
    }
}

MainApp.java

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

public class MainApp extends Application {
        private Stage primaryStage;

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

        @Override
        public void start(Stage primaryStage) throws Exception {
            this.primaryStage = primaryStage;

            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("MainWindow.fxml"));
            MainWindowController mainWindowController = new MainWindowController();
            mainWindowController.setMainApp(this);
            loader.setController(mainWindowController);
            Parent layout = loader.load();

            Scene scene = new Scene(layout);
            primaryStage.setScene(scene);
            primaryStage.show();
        }

        public Stage getPrimaryStage() {
            return primaryStage;
        }
}

MainWindowController.java

    import java.io.IOException;
    import java.net.URL;
    import java.util.HashMap;
    import java.util.ResourceBundle;

    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.fxml.Initializable;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Alert;
    import javafx.scene.control.Alert.AlertType;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.stage.Modality;
    import javafx.stage.Stage;

    public class MainWindowController extends AbstractController implements Initializable {

        @FXML private Button popitBtn;
        @FXML private Label resultLbl;

        @Override
        public void initialize(URL url, ResourceBundle rb) {
            resultLbl.setText("Lets get something in here");
            popitBtn.setOnAction((event)->{
                HashMap<String, Object> resultMap = showPopupWindow();
                resultLbl.setText("I've got this (username: "+resultMap.get("username")
                        +" /Password: "+resultMap.get("password")+")");
            });

        }


        private HashMap<String, Object> showPopupWindow() {
            HashMap<String, Object> resultMap = new HashMap<String, Object>();

            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("Popup.fxml"));
            // initializing the controller
            PopupController popupController = new PopupController();
            loader.setController(popupController);
            Parent layout;
            try {
                layout = loader.load();
                Scene scene = new Scene(layout);
                // this is the popup stage
                Stage popupStage = new Stage();
                // Giving the popup controller access to the popup stage (to allow the controller to close the stage) 
                popupController.setStage(popupStage);
                if(this.main!=null) {
                    popupStage.initOwner(main.getPrimaryStage());
                }
                popupStage.initModality(Modality.WINDOW_MODAL);
                popupStage.setScene(scene);
                popupStage.showAndWait();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return popupController.getResult();
        }
    }

PopupController.java

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

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class PopupController extends AbstractController implements Initializable {

    @FXML private TextField usernameTF;
    @FXML private PasswordField passwordPF;
    @FXML private Button connectBtn;
    private Stage stage = null;
    private HashMap<String, Object> result = new HashMap<String, Object>();

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        connectBtn.setOnAction((event)->{
            result.clear();
            result.put("username", usernameTF.getText());
            result.put("password", passwordPF.getText());
            closeStage();
        });

    }

    public HashMap<String, Object> getResult() {
        return this.result;
    }

    /**
     * setting the stage of this view
     * @param stage
     */
    public void setStage(Stage stage) {
        this.stage = stage;
    }

    /**
     * Closes the stage of this view
     */
    private void closeStage() {
        if(stage!=null) {
            stage.close();
        }
    }

}

这篇关于JavaFX新的自定义弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆