如何在JavaFx应用中打开新的窗口

有时候,我们可能有这样的需求——点击按钮时能够弹出新的窗口,然后在这个窗口中提供一些服务。那么在JavaFX中如何实现呢?

其实,在JavaFX弹出新的窗口原理很简单,就是再新建一个Stage,如下代码实现点击按钮打开新的窗口,即在按钮的鼠标点击事件中新建了一个Stage

package pre.huangjs.blog;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class OpenNewWindow extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {

        // 创建一个包含按钮的APP
        Button newWindowBtn = new Button("打开新的窗口");

        // 为按钮添加事件——点击时打开新的窗口
        newWindowBtn.setOnMouseClicked(new EventHandler<MouseEvent>() {
            public void handle(MouseEvent event) {

                // 创建新的stage
                Stage secondStage = new Stage();
                Label label = new Label("新窗口"); // 放一个标签
                StackPane secondPane = new StackPane(label);
                Scene secondScene = new Scene(secondPane, 300, 200);
                secondStage.setScene(secondScene);
                secondStage.show();
            }
        });

        StackPane mainPane = new StackPane(newWindowBtn); // 创建一个新的布局
        Scene scene = new Scene(mainPane, 500, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

01.gif

现在基本实现了我们的功能,但是还是存在一个不足,我们在关闭主窗口primaryStage时,打开的secondStage不会自动关闭,这个不太合理,如果想达到主窗口关闭时子窗口关闭,那么需要设置主窗口的setOnCloseRequest(EventHandler<WindowEvent> value)方法,具体代码:


    primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>(){
            public void handle(WindowEvent event) {
                Platform.exit();
            }
        });

02.gif

JavaFX初学者,如有术语使用不正确,请指正!

完整代码:

package pre.huangjs.blog;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class OpenNewWindow extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {

        // 创建一个包含按钮的APP
        Button newWindowBtn = new Button("打开新的窗口");

        // 为按钮添加事件——点击时打开新的窗口
        newWindowBtn.setOnMouseClicked(new EventHandler<MouseEvent>() {
            public void handle(MouseEvent event) {

                // 创建新的stage
                Stage secondStage = new Stage();
                Label label = new Label("新窗口"); // 放一个标签
                StackPane secondPane = new StackPane(label);
                Scene secondScene = new Scene(secondPane, 300, 200);
                secondStage.setScene(secondScene);
                secondStage.show();
            }
        });

        StackPane mainPane = new StackPane(newWindowBtn); // 创建一个新的布局
        Scene scene = new Scene(mainPane, 500, 400);
        primaryStage.setScene(scene);
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>(){
            public void handle(WindowEvent event) {
                Platform.exit();
            }
        });
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,812评论 25 709
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,648评论 9 468
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,626评论 0 17
  • 还想看更多文章的朋友可以访问我的个人博客 转载自人人网分享 想查看更多精彩文章,请关注我的 卷首语 Android...
    我是才子阅读 41,673评论 1 8
  • 我?是谁呀?不是你不是她,可“我”到底是个怎样的存在?不清楚。 一晃我就十七了,以前的有些东西我都模糊了甚至是忘记...
    程殊阅读 189评论 0 2