Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果滚动窗格小于视口,则扩展滚动窗格的内容_Java_Javafx - Fatal编程技术网

Java 如果滚动窗格小于视口,则扩展滚动窗格的内容

Java 如果滚动窗格小于视口,则扩展滚动窗格的内容,java,javafx,Java,Javafx,我想制作一个滚动窗格,里面有一个自定义窗格,它有两个子项。一个放着我的东西,一个只放在背景上。我想这样做,如果我缩小,并且内容比视口小,那么内容的大小将扩大,填充视口中的新位置。如果我向后缩放,它将保持不变,我现在在这个区域有了更大的内容。内容的新宽度为:originalWidth+viewportWidth-scaledWidth 我已经制作了网格,并且缩放工作正常,但是我无法使其调整内容的大小。我曾尝试在缩放到当前视口大小时设置内容大小,但不起作用 问题: 我做错了什么 布局是在fxml

我想制作一个滚动窗格,里面有一个自定义窗格,它有两个子项。一个放着我的东西,一个只放在背景上。我想这样做,如果我缩小,并且内容比视口小,那么内容的大小将扩大,填充视口中的新位置。如果我向后缩放,它将保持不变,我现在在这个区域有了更大的内容。内容的新宽度为:originalWidth+viewportWidth-scaledWidth

我已经制作了网格,并且缩放工作正常,但是我无法使其调整内容的大小。我曾尝试在缩放到当前视口大小时设置内容大小,但不起作用

问题:
  • 我做错了什么
布局是在fxml中定义的。另一个比滚动窗格内容设置填充高度和宽度没有什么不寻常的地方

自定义窗格类:
公共类CustomPane扩展StackPane实现可初始化{
@FXML
堆栈窗格视图;
@FXML
锚烷;
@FXML
网格窗格背景;
private DoubleProperty zoomFactor=新的SimpleDoubleProperty(1.5);
私有布尔属性altStatus=新的SimpleBoleanProperty(false);
公共自定义窗格(){
超级();
FXMLLoader=新的FXMLLoader();
setLocation(getClass().getClassLoader().getResource(“CustomCanvas.fxml”);
loader.setController(此);
setRoot(this);
试一试{
loader.load();
}捕获(IOE异常){
e、 printStackTrace();
}
}
@凌驾
公共void初始化(URL位置、ResourceBundle资源){
setStyle(“-fx背景色:透明”);
objectPane.prefWidthProperty().bind(prefWidthProperty());
objectPane.prefHeightProperty().bind(prefHeightProperty());
objectPane.getChildren().add(新圆圈(512、378、20、Color.RED));
}
公共无效缩放(滚动窗格父节点、节点节点、双因子、双x、双y){
时间线=新的时间线(60);
//确定规模
double oldScale=node.getScaleX();
双刻度=旧刻度*系数;
双f=(标度/旧标度)-1;
//确定我们必须移动节点的偏移量
Bounds-Bounds=node.localToScene(node.getBoundsInLocal());
double dx=(x-(bounds.getWidth()/2+bounds.getMinX());
double dy=(y-(bounds.getHeight()/2+bounds.getMinY());
//缩放和移动节点的时间线
timeline.getKeyFrames().clear();
timeline.getKeyFrames().addAll(
新关键帧(Duration.millis(100)、新关键值(node.translateProperty()、node.getTranslateX()-f*dx)),
新关键帧(Duration.millis(100)、新键值(node.translateYProperty()、node.getTranslateY()-f*dy)),
新关键帧(Duration.millis(100),新关键值(node.scaleProperty(),scale)),
新关键帧(Duration.millis(100),新KeyValue(node.scaleYProperty(),scale))
);
timeline.play();
Bounds viewportBounds=parent.getViewportBounds();
if(bounds.getWidth()
控制器类:
公共类控制器实现可初始化{
公共滚动窗格;
公共自定义窗格自定义窗格;
公共锚烷;
公共选项卡tab1;
@凌驾
公共void初始化(URL位置、ResourceBundle资源){
scrollPane.viewportBoundsProperty().addListener((可观察、旧值、新值)->{
setMinSize(newValue.getWidth(),newValue.getHeight());
});
scrollPane.requestLayout();
tab1.getTabPane().addEventFilter(按下KeyEvent.KEY,event1->{
if(event1.getCode()==KeyCode.ALT)
customPane.setAltStatus(true);
});
tab1.getTabPane().addEventFilter(KeyEvent.KEY_已发布,event1->{
if(event1.getCode()==KeyCode.ALT)
customPane.setAltStatus(false);
});
scrollPane.setOnScroll(事件->{
双变焦因子=1.5;

如果(event.getDeltaY()不确定我是否卸载了您想要实现的目标。但是如果您的目标是使滚动窗格的内容永远不会小于滚动窗格的宽度,那么这就为我完成了以下工作:

public class Zoom extends Application {

    @Override
    public void start(Stage primaryStage) {

        ImageView imageView = new ImageView(new Image(someImage));
        ScrollPane scrollPane = new ScrollPane(imageView);
        StackPane root = new StackPane(scrollPane);

        imageView.fitWidthProperty().bind(scrollPane.widthProperty());
        imageView.fitHeightProperty().bind(scrollPane.heightProperty());

        scrollPane.setOnScroll(evt -> {
            boolean zoomOut = evt.getDeltaY() < 0;
            double zoomFactor = zoomOut ? -0.2 : 0.2;

            imageView.setScaleX(imageView.getScaleX() + zoomFactor);
            imageView.setScaleY(imageView.getScaleY() + zoomFactor);

            if (zoomOut) {
                Bounds bounds = imageView.getBoundsInParent();
                if (bounds.getWidth() < scrollPane.getWidth()) {
                    imageView.setScaleX(1);
                    imageView.setScaleY(1);

                }
            }
        });

        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
公共类缩放扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
ImageView ImageView=新图像视图(新图像(someImage));
ScrollPane ScrollPane=新的滚动窗格(imageView);
StackPane root=新的StackPane(滚动窗格);
imageView.fitWidthProperty().bind(scrollPane.widthProperty());
imageView.fitHeightProperty().bind(scrollPane.heightProperty());
scrollPane.setOnScroll(evt->{
布尔zoomOut=evt.getDeltaY()<0;
双zoomFactor=zoomOut?-0.2:0.2;
setScaleX(imageView.getScaleX()+zoomFactor);
设置缩放(imageView.getScaleY()+zoomFactor);
如果(zoomOut){
Bounds Bounds=imageView.getBoundsInParent();
if(bounds.getWidth()public class Controller implements Initializable {
public ScrollPane scrollPane;
public CustomPane customPane;
public AnchorPane anchorPane;
public Tab tab1;

@Override
public void initialize(URL location, ResourceBundle resources) {
    scrollPane.viewportBoundsProperty().addListener((observable, oldValue, newValue) -> {
        customPane.setMinSize(newValue.getWidth(), newValue.getHeight());
    });
    scrollPane.requestLayout();

    tab1.getTabPane().addEventFilter(KeyEvent.KEY_PRESSED, event1 -> {
        if (event1.getCode() == KeyCode.ALT)
            customPane.setAltStatus(true);
    });
    tab1.getTabPane().addEventFilter(KeyEvent.KEY_RELEASED, event1 -> {
        if (event1.getCode() == KeyCode.ALT)
            customPane.setAltStatus(false);
    });

    scrollPane.setOnScroll(event -> {
        double zoomFactor = 1.5;
        if (event.getDeltaY() <= 0)
            zoomFactor = 1 / zoomFactor;
        customPane.setZoomFactor(zoomFactor);
        if (customPane.getAltStatus())
            customPane.zoom(scrollPane, customPane, customPane.getZoomFactor(), event.getSceneX(), event.getSceneY());
    });
}
}
public class GriddedPane extends Pane implements Initializable {

DoubleProperty gridWidth = new SimpleDoubleProperty(this, "gridWidth", 10);
DoubleProperty gridHeight = new SimpleDoubleProperty(this, "gridHeight", 10);

public GriddedPane() {
    super();
}

@Override
public void initialize(URL location, ResourceBundle resources) {

}

@Override
protected void layoutChildren() {
    getChildren().clear();
    setMouseTransparent(true);
    toBack();
    for (int i = 0; i < getHeight(); i += getGridWidth())
        getChildren().add(makeLine(0, i, getWidth(), i, "x"));
    for (int i = 0; i < getWidth(); i += getGridHeight())
        getChildren().add(makeLine(i, 0, i, getHeight(), "y"));
}

public void redrawLines() {
    for (Node n : getChildren()) {
        Line l = (Line) n;
        if (l.getUserData().equals("x")) {
            l.setEndX(getWidth());
        } else if (l.getUserData().equals("y")) {
            l.setEndY(getHeight());
        }
    }
}

private Line makeLine(double sx, double sy, double ex, double ey, String data) {
    final Line line = new Line(sx, sy, ex, ey);
    if (ex % (getGridWidth() * 10) == 0.0) {
        line.setStroke(Color.BLACK);
        line.setStrokeWidth(0.3);
    } else if (ey % (getGridHeight() * 10) == 0.0) {
        line.setStroke(Color.BLACK);
        line.setStrokeWidth(0.3);
    } else {
        line.setStroke(Color.GRAY);
        line.setStrokeWidth(0.1);
    }
    line.setUserData(data);
    return line;
}

public double getGridWidth() {
    return gridWidth.get();
}

public DoubleProperty gridWidthProperty() {
    return gridWidth;
}

public void setGridWidth(double gridWidth) {
    this.gridWidth.set(gridWidth);
}

public double getGridHeight() {
    return gridHeight.get();
}

public DoubleProperty gridHeightProperty() {
    return gridHeight;
}

public void setGridHeight(double gridHeight) {
    this.gridHeight.set(gridHeight);
}
}
public class Zoom extends Application {

    @Override
    public void start(Stage primaryStage) {

        ImageView imageView = new ImageView(new Image(someImage));
        ScrollPane scrollPane = new ScrollPane(imageView);
        StackPane root = new StackPane(scrollPane);

        imageView.fitWidthProperty().bind(scrollPane.widthProperty());
        imageView.fitHeightProperty().bind(scrollPane.heightProperty());

        scrollPane.setOnScroll(evt -> {
            boolean zoomOut = evt.getDeltaY() < 0;
            double zoomFactor = zoomOut ? -0.2 : 0.2;

            imageView.setScaleX(imageView.getScaleX() + zoomFactor);
            imageView.setScaleY(imageView.getScaleY() + zoomFactor);

            if (zoomOut) {
                Bounds bounds = imageView.getBoundsInParent();
                if (bounds.getWidth() < scrollPane.getWidth()) {
                    imageView.setScaleX(1);
                    imageView.setScaleY(1);

                }
            }
        });

        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

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