我有一个包含图像的
我在Windows 7上运行此代码:
TilePane
滚动面板,当我删除其中一张图片时,ScrollPane
会跳回到顶部,这在删除多个图像时非常烦人。是否有方法可以控制滚动行为?我在Windows 7上运行此代码:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class ScrollbarProblem extends Application{
private TilePane imagePane;
private ScrollPane scroller;
@Override
public void start(Stage primaryStage) {
imagePane = new TilePane();
scroller = new ScrollPane();
scroller.setContent(imagePane);
BorderPane root = new BorderPane(scroller, null, null, null, null);
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
for(int i = 0; i < 20; i++){
Image img = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/SIPI_Jelly_Beans_4.1.07.tiff/lossy-page1-220px-SIPI_Jelly_Beans_4.1.07.tiff.jpg");
final ImageView imageView = new ImageView(img);
final Button button = new Button("Delete");
final StackPane stackPane = new StackPane();
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
imagePane.getChildren().remove(stackPane);
}
});
stackPane.getChildren().addAll(imageView, button);
imagePane.getChildren().add(stackPane);
}
}
public static void main(String[] args) {
launch(args);
}
}