gpt4 book ai didi

JavaFX 高度调整问题

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:58 25 4
gpt4 key购买 nike

我正在尝试在选项卡中获取 WebViewHeightWidth 来调整大小。 宽度起作用,但高度不起作用。 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++;
}
}

最佳答案

宽度有效,因为Scene宽度始终等于Content宽度,因此您的问题,高度不同,因为您添加了MenuBar(menuBar)

所以在你的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应该是您的根,您可以在其中添加 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;

关于JavaFX 高度调整问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42044969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com