JavaFX 高度调整问题
JavaFX height resize issue
我正在尝试在要调整大小的选项卡中获取 WebView
的 Height
和 Width
。 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++;
}
}
宽度有效,因为 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()
也改变所有这些,它很难理解,你可以 return 从这里只选择标签,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;
我正在尝试在要调整大小的选项卡中获取 WebView
的 Height
和 Width
。 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++;
}
}
宽度有效,因为 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()
也改变所有这些,它很难理解,你可以 return 从这里只选择标签,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;