我遇到了将ImageView节点集中在以下Browser对象中的问题。我提供了以下代码的片段。我的想法是使用setStyle(“.....”),但这似乎没有对图像的位置产生任何影响(它仍然显示在左上角)。任何建议都非常感谢。谢谢!
class Browser extends Region {
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
File file;
ImageView image;
public Browser(String fileName, Image loaderImage) throws MalformedURLException {
file = new File(fileName);
//apply the styles
getStyleClass().add("browser");
//add the web view to the scene
getChildren().add(browser);
// load default image
image = new ImageView();
image.setImage(loaderImage);
setStyle("-fx-background-position: CENTER;");
getChildren().add(image);
}
答案 0 :(得分:2)
根据Javadocs:
默认情况下,Region继承其超类
Parent
的布局行为,这意味着它会将任何可调整大小的子节点的大小调整为其首选大小,但不会重新定位它们。如果应用程序需要更具体的布局行为,那么它应该使用Region
子类之一:StackPane
,HBox
,VBox
,TilePane
,{{1 }},FlowPane
,BorderPane
或GridPane
。
所以使用其中一个可能会有更好的运气。