JavaFX WebView调整大小问题

时间:2017-02-05 17:16:18

标签: java javafx webview

如何正确调整网页视图的大小?当窗口缩小到较小的大小时,它似乎将WebView推到帧的末尾,而不实际调整大小。如何正确地执行此操作,允许调整窗口大小并使WebView适当地适合框架。

public class OuroborosViewer extends Application {

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

@Override
public void start(Stage stage) {
    stage.setTitle("Ouroboros");
    Scene scene = new Scene(new VBox(), 1920, 1080);
    scene.setFill(Color.OLDLACE);

    MenuBar menuBar = new MenuBar();
    Menu menuFile = new Menu("File");
    Menu menuEdit = new Menu("Edit");
    Menu menuView = new Menu("View");

    menuBar.getMenus().addAll(menuFile, menuEdit, menuView);
    ((VBox) scene.getRoot()).getChildren().addAll(menuBar);

    BorderPane borderPane = createTab(scene);
    ((VBox) scene.getRoot()).getChildren().add(borderPane);

    scene.widthProperty().addListener(new WidthListener());
    scene.heightProperty().addListener(new HeightListener());
    //  stage.getIcons().add(new Image("Ouroboros.svg"));
    stage.setScene(scene);
    try {
        sleep(500);
    } catch (InterruptedException ex) {
        Logger.getLogger(OuroborosViewer.class.getName()).log(Level.SEVERE, null, ex);
    }

    stage.show();
}

private BorderPane createTab(Scene scene) {
    TabPane tabPane = new TabPane();
    BorderPane borderPane = new BorderPane();

    Tab tab = new Tab();
    tab.setText("Google");
    //VBox hbox = new VBox(new BrowserToolbar(scene), new OuroborosBrowser(scene));
    VBox vbox = new VBox();
    BrowserToolbar toolbar = new BrowserToolbar(scene);
    OuroborosBrowser ob = new OuroborosBrowser(scene);
    VBox.setVgrow(toolbar, Priority.ALWAYS);
    VBox.setVgrow(ob, Priority.ALWAYS);
    vbox.getChildren().addAll(toolbar, ob);
    vbox.autosize();

    tabPane.getTabs().add(tab);
    tab.setContent(vbox);
    borderPane.setTop(tabPane);
    return borderPane;
}

private static class WidthListener implements ChangeListener<Number> {

    @Override
    public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneWidth, Number newSceneWidth) {
        OuroborosBrowser.browsers[0].setPrefWidth(newSceneWidth.doubleValue());
        BrowserToolbar.URLBars[0].setPrefWidth(newSceneWidth.doubleValue() - 115);
        BrowserToolbar.toolBars[0].setPrefWidth(newSceneWidth.doubleValue());
    }
}

private static class HeightListener implements ChangeListener<Number> {

    @Override
    public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight, Number newSceneHeight) {
        if (oldSceneHeight != newSceneHeight) {
            try {
                OuroborosBrowser.browsers[0].setPrefHeight(newSceneHeight.doubleValue()
                        + OuroborosBrowser.browsers[0].getScene().getRoot().getChildrenUnmodifiable()
                                .get(0).prefHeight(0));
            } catch (Exception ex) {

            }
        }
    }
}

}

class OuroborosBrowser extends Region {
public static int browserCounter = 0;
public static WebView[] browsers = new WebView[25];
private static WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();


public OuroborosBrowser(Scene scene) { 
    browser.setPrefSize(scene.getWidth(), scene.getHeight());
    webEngine.load("http://www.google.com/index.html");
    getChildren().add(browser);
    browsers[browserCounter] = browser;
    browserCounter++;
}
}



public class BrowserToolbar extends Region {

public static TextField[] URLBars = new TextField[25];
public static ToolBar[] toolBars = new ToolBar[25];
public static int barCounter = 0;

public BrowserToolbar(Scene scene) {
    Button backButton = new Button("  <  ");
    Button forwardButton = new Button("  >  ");
    TextField URLbar = new TextField();
    URLbar.setPrefWidth(scene.getWidth() - 115);
    URLBars[barCounter] = URLbar;
    URLbar.setPrefWidth(2000);
    final ToolBar toolbar = new ToolBar(
            backButton, forwardButton, URLbar);
    toolbar.setPrefWidth(2000);
    toolBars[barCounter] = toolbar;
    barCounter++;

    HBox hb = new HBox();
    hb.getChildren().add(toolbar);
    hb.setPrefWidth(scene.getWidth());
    hb.autosize();
    getChildren().add(hb);
}
}

下图中阴影区域是随帧高减小而不断增加的区域。

link to image

0 个答案:

没有答案