JavaFX高度调整大小问题

时间:2017-02-04 20:01:14

标签: java javafx javafx-webengine

我正在尝试在选项卡中同时获取Width的{​​{1}}和WebView来调整大小。 Width工作的高度不高。 System.out.println();显示高度输入正确,但您只能在顶部看到一点点。如何制作高度将像宽度一样缩放?

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(), 1200, 800);
    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);
    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();
    hbox.setAlignment(Pos.CENTER);
    tab.setContent(hbox);
    tabPane.getTabs().add(tab);

    tab.setContent(new OuroborosBrowser(scene));
    // bind to take available space

    borderPane.setCenter(tabPane);
    return borderPane;
}

private static class WidthListener implements ChangeListener<Number> {

    @Override
    public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneWidth, Number newSceneWidth) {
        String no = String.valueOf(newSceneWidth);
        double width = Double.parseDouble(no);
        System.out.println("Width : " + width);
        OuroborosBrowser.browsers[0].setPrefWidth(width);
    }
}

private static class HeightListener implements ChangeListener<Number> {

    @Override
    public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight, Number newSceneHeight) {
        String no = String.valueOf(newSceneHeight);
        double height = Double.parseDouble(no);
        System.out.println("Height : " + height);
        OuroborosBrowser.browsers[0].setPrefHeight(height);
    }
}
}

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();
private static double browserWidth = 1200;
private static double browserHeight = 800;

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

1 个答案:

答案 0 :(得分:0)

宽度有效,因为Scene宽度始终等于Content宽度因此您的问题,因为您添加了MenuBarmenuBar)< / p>

所以在HeightListener执行此操作

System.out.println("Height : " + newSceneHeight);
OuroborosBrowser.browsers[0].setPrefHeight(newSceneHeight.doubleValue() 
    - OuroborosBrowser.browsers[0].getScene().getRoot().getChildrenUnmodifiable()
           .get(0).prefHeight(-1));//all this nonsense to get the menuBar

也是这些

    String no = String.valueOf(newSceneWidth); //no need
    double width = Double.parseDouble(no); // no need use newSceneHeight.doubleValue()
    System.out.println("Width : " + width); //why not use no rather ??, no need
    OuroborosBrowser.browsers[0].setPrefWidth(width);//use newSceneHeight.doubleValue()

也改变所有这些,很难理解,你只能从这里返回标签,BorderPane应该是你的根,你可以在menuBar添加setTop(node)

TabPane tabPane = new TabPane();
BorderPane borderPane = new BorderPane();

Tab tab = new Tab();
tab.setText("Google");
VBox hbox = new VBox();//you are not using this
hbox.setAlignment(Pos.CENTER);
tab.setContent(hbox);//you replaced this
tabPane.getTabs().add(tab);

tab.setContent(new OuroborosBrowser(scene));//you see?
// bind to take available space

borderPane.setCenter(tabPane); // why this? its not needed
return borderPane;