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();

       

    }

   

}

posted @ 2015-01-31 18:15  坤山五号土黄金  阅读(1176)  评论(0编辑  收藏  举报