JavaFX新的自定义弹出窗口

JavaFX新的自定义弹出窗口,java,javafx,dialog,Java,Javafx,Dialog,我正在搜索JavaFX中弹出窗口的示例。 我有一个JavaFX应用程序,我需要一个弹出窗口。这个弹出窗口需要一些复杂的输入,我需要处理和检查这些输入,然后返回主应用程序/窗口 现在的问题是我在任何地方都找不到一个例子,如何在一个JavaFX控制器类中调用现在的JavaFX弹出窗口?我只找到了examle如何创建对话框弹出窗口,但我找不到基于JavaFX的新弹出窗口示例(我看到了一个解决方案,其中有两个窗口并行,但我只需要在需要时创建一个) 您知道JavaFx自定义弹出窗口的这种示例吗?我想我理解

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

现在的问题是我在任何地方都找不到一个例子,如何在一个JavaFX控制器类中调用现在的JavaFX弹出窗口?我只找到了examle如何创建对话框弹出窗口,但我找不到基于JavaFX的新弹出窗口示例(我看到了一个解决方案,其中有两个窗口并行,但我只需要在需要时创建一个)


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

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

  • 我已经创建了两个FXML文件,一个用于主窗口(MainWindow.FXML),一个用于弹出窗口(popup.FXML)
  • 为每个fxml文件创建了两个控制器类,这些控制器扩展了一个AbstractController类,所有有趣的事情都进入了这两个控制器
  • 抽象控制器类只有一个方法,允许具体控制器访问主应用程序
  • 在mainApp类中没有什么特别之处,只是加载主窗口的控制器并将主窗口设置为主舞台场景的根

main window.fxml

MainApp.java

public abstract class AbstractController {

    protected MainApp main;

    public void setMainApp(MainApp main) {
        this.main = main;
    }
}
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;
        }
}
    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();
        }
    }
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();
        }
    }

}
MainWindowController.java

public abstract class AbstractController {

    protected MainApp main;

    public void setMainApp(MainApp main) {
        this.main = main;
    }
}
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;
        }
}
    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();
        }
    }
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();
        }
    }

}
import java.io.IOException;
导入java.net.URL;
导入java.util.HashMap;
导入java.util.ResourceBundle;
导入javafx.fxml.fxml;
导入javafx.fxml.fxmloader;
导入javafx.fxml.Initializable;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.scene.control.Alert;
导入javafx.scene.control.Alert.AlertType;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.stage.model;
导入javafx.stage.stage;
公共类MainWindowController扩展AbstractController实现可初始化{
@FXML专用按钮popitBtn;
@FXML私有标签resultbl;
@凌驾
公共void初始化(URL、ResourceBundle rb){
resultbl.setText(“让我们在这里得到一些东西”);
popitBtn.setOnAction((事件)->{
HashMap resultMap=showPopupWindow();
resultbl.setText(“我得到了这个(用户名:”+resultMap.get(“用户名”)
+/Password:“+resultMap.get”(“Password”)+”;
});
}
私有HashMap showPopupWindow(){
HashMap resultMap=新的HashMap();
FXMLLoader=新的FXMLLoader();
setLocation(getClass().getResource(“Popup.fxml”);
//初始化控制器
PopupController PopupController=新的PopupController();
loader.setController(popupController);
父布局;
试一试{
layout=loader.load();
场景=新场景(布局);
//这是弹出阶段
Stage popustage=新Stage();
//允许弹出控制器访问弹出阶段(允许控制器关闭阶段)
popupController.setStage(PopupUpStage);
if(this.main!=null){
initOwner(main.getPrimaryStage());
}
popumpstage.initmodal(MODAL.WINDOW_MODAL);
popupStage.setScene(场景);
popupStage.show和wait();
}捕获(IOE异常){
e、 printStackTrace();
}
返回popupController.getResult();
}
}
PopupController.java

public abstract class AbstractController {

    protected MainApp main;

    public void setMainApp(MainApp main) {
        this.main = main;
    }
}
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;
        }
}
    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();
        }
    }
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();
        }
    }

}
import java.net.URL;
导入java.util.HashMap;
导入java.util.ResourceBundle;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.control.Button;
导入javafx.scene.control.PasswordField;
导入javafx.scene.control.TextField;
导入javafx.stage.stage;
公共类PopupController扩展AbstractController实现可初始化{
@FXML私有文本字段usernameTF;
@FXML私有密码字段passwordPF;
@FXML专用按钮连接BTN;
私有阶段=空;
private HashMap result=new HashMap();
@凌驾
公共void初始化(URL、ResourceBundle rb){
connectBtn.setOnAction((事件)->{
result.clear();
put(“username”,usernameTF.getText());
put(“password”,passwordPF.getText());
closeStage();
});
}
公共HashMap getResult(){
返回此结果;
}
/**
*设置此视图的舞台
*@param阶段
*/
公共空间设置阶段(阶段){
这个阶段=阶段;
}
/**
*关闭此视图的阶段
*/
私人舞台(){
如果(阶段!=null){
stage.close();
}
}
}

哈立德·萨布的答案很好。如果使用sceneBuilder创建fxml文件,请不要设置setController(在函数showPopupUpWindow()中)

只要使用-

FXMLLoader=newFXMLLoader(getClass().getResource(“fxml.file”)


因为fxml文件会自动初始化相关类

我猜你的意思是弹出…你可以把你的弹出内容放在一个单独的阶段,并在必要时打开这个阶段。在弹出窗口中还有一个“OK”(确定)按钮来关闭这个
阶段。这可能会有帮助,谢谢,是的弹出窗口请看一下这篇文章:是的,这正是我要找的。谢谢!不客气:),如果您认为正确,请不要忘记将此答案标记为正确答案;)这个例子很好用,github文件也很好用。