javafx画面间跳转

javafx中一个JVM进程只能存在一个Application类,这个Application类只能调用一次launch()方法来启动它。

那我们如果启动一个新的窗口呢?

javafxStage类继承了Window代表着一个窗口,所以我们只需要构造一个Stage并将之显示即可。

package mygosecond;

 

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.layout.StackPane;

import javafx.stage.Stage;

 

/**

 *

 * @author quchengrui

 */

public class MyGoSecondStage extends Application {

   

    @Override

    public void start(Stage primaryStage) {

        Button btn = new Button();

        btn.setText("Say 'Hello World'");

        btn.setOnAction(new EventHandler<ActionEvent>() {

           

            @Override

            public void handle(ActionEvent event) {

                System.out.println("Hello World!");

                //跳转到另外一个画面,当前画面关闭

                primaryStage.close();

                new SencondStage().show();          

            }

        });

       

        StackPane root = new StackPane();

        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");

        primaryStage.setScene(scene);

        primaryStage.show();

    }

 

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        launch(args);

    }

  

}

 

package mygosecond;

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.layout.StackPane;

import javafx.stage.Stage;

 

/**

 *

 * @author quchengrui

 */

public class SencondStage extends Stage {

      Button btn = new Button();

       

    public  SencondStage()       

    {

      btn.setText("Say ' hello  China'");

      StackPane root = new StackPane();

        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        setTitle("Hello China!");

        this.setScene(scene);

        this.show();

       

    }

   

}

转载于:https://www.cnblogs.com/quchengrui/p/4264436.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值