2015-07-21 33 views
3

我想重新調整大小的圖像,使其適合(縮放)由SplitPane分配的空間(解決方法建議:此圖像將在運行程序時更改,它應該從文本中生成)。我目前使用的ImageView改變了SplitPaneDivider的位置。分頻器永遠不會導致ImageView減少超過圖像的大小。JavaFx ImageView不會調整fitWidthProperty綁定到VBox

Two images showing this behavior

ImageView fitWidthProperty鏈接到VBox並沒有給我預期的調整行爲。增加ImageView的大小確實有效(在沒有綁定的情況下修復它),但圖像永遠不會增長,它只是獲得邊界。

我MWE:

sample.fxml

<?import javafx.scene.control.SplitPane?> 
<?import javafx.scene.control.TextArea?> 
<?import javafx.scene.image.ImageView?> 
<?import javafx.scene.layout.HBox?> 
<?import javafx.scene.layout.VBox?> 
<SplitPane orientation="HORIZONTAL" xmlns:fx="http://javafx.com/fxml"> 
    <TextArea fx:id="textArea"/> 
    <VBox fx:id="vBox" alignment="CENTER"> 
     <HBox fx:id="hBox" alignment="CENTER"> 
      <ImageView fx:id="imageView"/> 
     </HBox> 
    </VBox> 
</SplitPane> 

Controller.java

import javafx.fxml.FXML; 
import javafx.scene.control.TextArea; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 

public class Controller { 
    @FXML 
    private TextArea textArea = new TextArea(); 
    @FXML 
    private VBox vBox = new VBox(); 
    @FXML 
    private HBox hBox = new HBox(); 
    @FXML 
    private ImageView imageView = new ImageView(); 

    public Controller() { 
     imageView.fitWidthProperty().bind(vBox.widthProperty()); 
     imageView.fitHeightProperty().bind(hBox.heightProperty()); 
    } 

    public void start() { 
     textArea.setText("text"); 

     Image image = new Image("https://www.google.nl/images/srpr/logo11w.png"); 
     imageView.setImage(image); 
    } 
} 

Main.java

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Main extends Application { 

    @Override 
    public void start(final Stage primaryStage) throws Exception { 
     final Controller controller = new Controller(); 

     FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml")); 
     fxmlLoader.setController(controller); 

     final Parent root = fxmlLoader.load(); 
     primaryStage.setTitle("MWE"); 
     final Scene scene = new Scene(root, 640, 480); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

     controller.start(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

回答

2

你可以嘗試結合蛋白將ImageView的fitWidthProperty()添加到SplitPane的divider位置屬性中。

綁定類似這應該工作:

imageView.fitWidthProperty().bind(Bindings.subtract(1, 
       splitPane.getDividers().get(0).positionProperty()) 
             .multiply(splitPane.widthProperty()); 
+0

我已經嘗試了這個建議,它的工作原理有點,但它會導致'ImageView'增長(約30像素)時,'TextArea'被選中,點擊並輸入。關於移動分隔符的調整大小行爲是我想要的。 – user1658374

1

這是我對你的樣本!

<?import javafx.scene.control.SplitPane?> 
<?import javafx.scene.image.Image?> 
<?import javafx.scene.image.ImageView?> 
<?import javafx.scene.layout.StackPane?> 
<?import javafx.scene.layout.VBox?> 
<StackPane fx:controller="sample.Controller" 
     xmlns:fx="http://javafx.com/fxml" alignment="center"> 

<SplitPane fx:id="splitpane"> 
    <VBox fx:id="left" prefWidth="300" prefHeight="200" style="-fx-background-color: antiquewhite"> 
     <ImageView fx:id="image1" preserveRatio="true" pickOnBounds="true"> 
      <image> 
       <Image url="@/sample/test.jpg"/> 
      </image> 
     </ImageView> 
    </VBox> 
    <VBox fx:id="right" prefWidth="300" prefHeight="300" style="-fx-background-color: aquamarine"> 
     <ImageView fx:id="image2" preserveRatio="true" pickOnBounds="true"> 
      <image> 
       <Image url="@/sample/test.jpg"/> 
      </image> 
     </ImageView> 
    </VBox> 
</SplitPane> 

package sample; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.SplitPane; 
import javafx.scene.image.ImageView; 
import java.net.URL; 
import java.util.ResourceBundle; 

public class Controller implements Initializable { 

    @FXML 
    ImageView image1; 
    @FXML 
    ImageView image2; 
    @FXML 
    SplitPane splitpane; 
    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
    splitpane.getDividers().get(0).positionProperty().addListener((observable, oldValue, newValue) -> { 
     System.out.println(newValue); 
     int w = 600; 
     double w1 = w * newValue.doubleValue(); 
     double w2 = w - w1; 
     image1.setFitWidth(w1); 
     image2.setFitWidth(w2); 
    }); 
    } 
} 

run result