AnchorPane shows a white border when setResizable

2019-09-06 02:02发布

My problem is that when I use setResizable(false) one white border is shown on my scene. When I don't set it false works fine, but I need to set it false.

Code:

 Scene scene = new Scene(new StackPane());  

        LoginManager loginManager = new LoginManager(scene);
        loginManager.showLoginScreen();
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.show();

My ImageView is inside of my AnchorPane

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="900.0" xmlns:fx="http://javafx.com/fxml" fx:controller="br.com.facilit.target.app.desktop.view.MainViewController">
  <children>
    <ImageView id="background" fitHeight="600.0" fitWidth="900.0" pickOnBounds="true">
      <image>
        <Image url="@../images/background.jpg" preserveRatio="true" smooth="true" />
      </image>

enter image description here

3条回答
【Aperson】
2楼-- · 2019-09-06 02:26

I had the same issue, I resolved in this way:

primaryStage.setScene(scene);

primaryStage.setWidth(900);
primaryStage.setHeight(600);
primaryStage.setResizable(false);

primaryStage.show();

You have to set the scene before setting the resizable property and set the width/height manually. This has solved my problem.

查看更多
孤傲高冷的网名
3楼-- · 2019-09-06 02:40
  1. Set maxSize() and minSize to the size of the image:

    Scene scene = new Scene(new StackPane());  
    
    private final static double IMG_WIDTH = 900d;
    private final static double IMG_HEIGHT = 600d;
    
    LoginManager loginManager = new LoginManager(scene);
    loginManager.showLoginScreen();
    
    primaryStage.setResizable(false);
    primaryStage.setMaxWidth(IMG_WIDTH);
    primaryStage.setMaxHeight(IMG_HEIGHT);
    primaryStage.setMinWidth(IMG_WIDTH);
    primaryStage.setMinHeight(IMG_HEIGHT);
    
    primaryStage.setScene(scene);
    primaryStage.show();
    
  2. Check the size of the image if it really is 900x600.

查看更多
Luminary・发光体
4楼-- · 2019-09-06 02:42

After try too many things, I discovered how to fix it.

I don't know why but when you set resible false, the screen automaticaly gains a extra space below and in the right side. To fix it, you should set the image background bigger than screen width and height, for example if you are using 900x600 in your anchor pane, you should set your image to 920x620.

One more time, I don't know why is it happening, but this was the only way that I've found to fix it whitout using css.

查看更多
登录 后发表回答