JavaFX之实现桌面应用的界面跳转

来源:互联网 发布:淘宝上正规药店是哪个 编辑:程序博客网 时间:2024/06/03 16:49

只是可以实现,但是这种跳转方法是否规范我也不是很清楚疑问疑问疑问

例如:点击Jfx2.java创建舞台中的Button跳转到Jfx3.java创建的舞台上




代码如下:

Jfx2.java

package review;import javafx.application.*;import javafx.scene.*;import javafx.stage.*;import javafx.scene.control.*;import javafx.scene.layout.*;public class Jfx2 extends Application{public static void main(String[] args){launch(args);}@Overridepublic void start(Stage stage) throws Exception{StackPane pane=new StackPane();Button button=new Button("Open another stage");button.setCursor(Cursor.HAND);button.setOnAction(e->{Jfx3 open=new Jfx3();try{open.start(new Stage());stage.hide(); } catch (Exception e1){e1.printStackTrace();}});pane.getChildren().add(button);Scene scene=new Scene(pane,300,200);stage.setScene(scene);stage.setTitle("Demo");stage.show();}}

Jfx3.java

package review;import javafx.application.*;import javafx.scene.*;import javafx.stage.*;import javafx.scene.control.*;import javafx.scene.layout.*;public class Jfx3 extends Application{@Overridepublic void start(Stage stage) throws Exception{StackPane pane=new StackPane();Label lb=new Label("This is a new stage");lb.setCursor(Cursor.HAND);pane.getChildren().add(lb);Scene scene=new Scene(pane,300,200);stage.setScene(scene);stage.setTitle("Demo");stage.show();}}


关键部分是

button.setOnAction(e->{Jfx3 open=new Jfx3();try{open.start(new Stage());stage.hide();} catch (Exception e1){e1.printStackTrace();}});


调用Jfx3.java(所要跳转到界面)的start()方法以实现跳转界面的目的;

如果需要让上一层的界面不显示,可以调用hide()方法来“隐藏”上一层界面,hide()方法文档中的解释是:Attempts to hide this Window by setting the visibility to false. 意思就是”试图通过设置可见性为false来隐藏此窗口“。但是直接”隐藏“是否合理我也难以判断,欢迎大家提供建议。

阅读全文
0 0
原创粉丝点击