JavaFXImageView赢得了';无法在fitWidthProperty绑定到VBox时调整大小
我正在尝试重新调整图像的大小,使其适合(缩放)由JavaFXImageView赢得了';无法在fitWidthProperty绑定到VBox时调整大小,javafx,imageview,java-8,fxml,splitpane,Javafx,Imageview,Java 8,Fxml,Splitpane,我正在尝试重新调整图像的大小,使其适合(缩放)由拆分窗格分配的空间(对于解决方案建议:此图像在运行程序时将发生更改,它应根据文本生成)。我当前使用的ImageView更改了分割窗格的位置。切割器决不能使ImageView减小到超过图像的大小 将ImageViewfitWidthProperty链接到VBox并没有给我预期的调整大小行为。增加ImageView的大小确实有效(在没有绑定的情况下它是固定的),但是图像永远不会增长,它只是在它周围有边框 我的MWE: sample.fxml: <
拆分窗格
分配的空间(对于解决方案建议:此图像在运行程序时将发生更改,它应根据文本生成)。我当前使用的ImageView更改了分割窗格的位置。切割器决不能使ImageView
减小到超过图像的大小
将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>
和Main.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);
}
}
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);
}
}
您可以尝试将ImageView的
fitWidthProperty()
绑定到SplitPane的分隔符位置属性
类似于此的绑定应适用于:
imageView.fitWidthProperty().bind(Bindings.subtract(1,
splitPane.getDividers().get(0).positionProperty())
.multiply(splitPane.widthProperty());
这是我给你的样品
<?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>
我已经尝试过这个建议,它在某种程度上是有效的,但是当选择
文本区域,单击并键入时,它会导致图像视图
增加(大约30个像素)。关于移动分隔符的调整大小行为是我想要的。
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);
});
}
}