javafx如何实现模态/模式对话框

来源:互联网 发布:销售数据分析怎么写 编辑:程序博客网 时间:2024/06/03 11:05

注:打开的模式对话框内是一个嵌套的html页面。(个人需求,和模式对话框没什么关系)
一、模式对话框代码:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class ModalDialog extends Application {
    /**
     * 网页地址
     */
    private String url;

    public ModalDialog(final Stage stg, String url) {
        this.url = url;
        final Stage stage = new Stage();
        // Initialize the Stage with type of modal
        stage.initModality(Modality.APPLICATION_MODAL);
        // Set the owner of the Stage
        stage.initOwner(stg);
        stage.setTitle("Top Stage With Modality");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250, Color.LIGHTGREEN);
        stage.setScene(scene);
        stage.setTitle("测试页面");
        try {
            start(stage);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        stage.show();
    }

    @Override
    public void start(Stage stage) throws Exception {
        // TODO Auto-generated method stub
        stage.setWidth(600);
        stage.setHeight(700);
        stage.setTitle("测试页面");
        Scene scene = new Scene(new Group());
        final WebView browser = new WebView();
        final WebEngine webEngine = browser.getEngine();
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(browser);
        webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
            @Override
            public void changed(ObservableValue ov, State oldState, State newState) {
                if (newState == Worker.State.SUCCEEDED) {

                }

            }
        });
        webEngine.load(url);
        scene.setRoot(scrollPane);
        stage.setScene(scene);
        stage.show();
    }

}

二、调用代码:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ModalTest extends Application {
    /** * @param args the command line arguments */
    public static void main(String[] args) {
        Application.launch(ModalTest.class, args);
    }

    @Override
    public void start(final Stage primaryStage) {
        primaryStage.setTitle("Hello World");
        Group root = new Group();
        Scene scene = new Scene(root, 900, 700, Color.LIGHTBLUE);
        Button btn = new Button();
        btn.setLayoutX(250);
        btn.setLayoutY(240);
        btn.setText("Show modal dialog");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
             ModalDialog md = new ModalDialog(primaryStage,"www.baidu.com");
            }
        });
        root.getChildren().add(btn);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}



0 0
原创粉丝点击