2017-02-04 163 views
1

我正在嘗試在選項卡中調整和WidthWebView以調整大小。 Width工作的高度沒有。 System.out.println();顯示高度輸入正確,但只能看到頂部。我如何製作高度會像寬度一樣縮放?JavaFX高度調整大小問題

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++; 
} 
} 
+0

我不明白這_but你只能在頂部看到一點點?你什麼意思? – Elltz

+0

那麼,當高度監聽器被註釋掉時,它會按預期工作,只是不可縮放,當它不是可見的時候,只有一小部分可見,只有一個像素或兩個大小在框架的寬度上延伸。 –

回答

0

寬度起作用是因爲Scene寬度總是等於Content寬度,因此您的問題,高度是不同的,因爲你在HeightListener加入MenuBarmenuBar

這樣做

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應該是你的根,在這裏你可以添加menuBarsetTop(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;