这里我想创建一个响应按钮以及imageview,但问题是imageview没有正确绑定按钮,如果我点击按钮或尝试调整窗口大小,自动图像大小自动化和许多无关紧要的缩放问题发生,尽管我没有为该按钮设置一个事件处理程序。
这是我的示例代码
public class ImageController extends Application{
private DoubleProperty fontSize = new SimpleDoubleProperty(10);
@Override
public void start(Stage primaryStage) {
FlowPane fp=new FlowPane(); //here i used flowpane to make imagegallery(productview) that will be in responsive in thier position and i tested my problem not with the flowpane
for(int j=0;j<1;j++)
{
try {
Button button = new Button();
button.styleProperty().bind(Bindings.concat("-fx-font-size: ", fontSize.asString(), ";")); //button size binded to the scene
FileInputStream input = new FileInputStream("images/wine.png");
Image image = new Image(input, 100, 100, true,true);
ImageView imageView=new ImageView(image);
imageView.setPreserveRatio(true);
imageView.fitWidthProperty().bind(button.widthProperty()); //image size binded to the button
imageView.fitHeightProperty().bind(button.heightProperty());
button.setGraphic(imageView);
fp.getChildren().add(button);
} catch (FileNotFoundException ex) {
Logger.getLogger(ImageController.class.getName()).log(Level.SEVERE, null, ex);
}
}
Scene scene = new Scene(fp, 500, 500);
fontSize.bind(scene.widthProperty().add(scene.heightProperty()).divide(50));
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
你可以通过运行我的代码并调整窗口大小或点击按钮来直接看到我的问题。我需要一个按钮和imageview都会根据屏幕调整大小(响应)调整大小。谢谢你提前