如何使用JavaFX垂直堆叠下载窗格?

如何使用JavaFX垂直堆叠下载窗格?,java,javafx,pane,Java,Javafx,Pane,我有一个返回窗格的方法,该窗格上有一些按钮和进度条,用于呈现挂起下载的进度,同时允许用户暂停、恢复或取消下载: 我的问题是,每当我开始一个新的下载时,我都想在最后一个窗格的下方添加一个新的窗格,在新的下载开始时垂直堆叠它们。然而,我创建的逻辑会在与上一个窗格相同的位置打开一个新窗格-覆盖它 如何使这些窗格堆叠 我试过的 当前显示的代码仅描述如何创建“窗格”,而不是如何将它们对齐/放置到父容器/窗口中 您对ProgressClass-窗格的元素使用绝对定位,我猜您在“外部”上也会这样做 修复:改

我有一个返回窗格的方法,该窗格上有一些按钮和进度条,用于呈现挂起下载的进度,同时允许用户暂停、恢复或取消下载:

我的问题是,每当我开始一个新的下载时,我都想在最后一个窗格的下方添加一个新的窗格,在新的下载开始时垂直堆叠它们。然而,我创建的逻辑会在与上一个窗格相同的位置打开一个新窗格-覆盖它

如何使这些窗格堆叠

我试过的
当前显示的代码仅描述如何创建“窗格”,而不是如何将它们对齐/放置到父容器/窗口中

您对
ProgressClass
-窗格的元素使用绝对定位,我猜您在“外部”上也会这样做

修复:改用布局容器

在包含其他元素的外部窗口中,添加一个
javafx.scene.layout.VBox
,而不是直接添加创建的
ProgressClass

每次添加另一个
ProgressClass
-实例时,将其作为子实例添加到
javafx.scene.layout.VBox
,它将自动垂直堆叠
ProgressClass
的各个实例


您可能不想访问一些布局教程来练习使用比自己放置每个元素更强大的布局容器

见f.e.:和

package DownloadManager;

import java.io.File;
import java.util.ArrayList;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ProgressClass {

    public Pane show( DownloadAction ob,Button pause,Button resume,Button delete,Thread thread) 
    {


        File resumefile = new File("C:\\Users\\kamran ali\\Desktop\\play.png");
        File pausefile = new File("C:\\Users\\kamran ali\\Desktop\\pause1.png");
        File deletefile = new File("C:\\Users\\kamran ali\\Desktop\\delete1.png");


        Image resIcon = new Image(resumefile.toURI().toString());
        Image paseIcon = new Image(pausefile.toURI().toString());
        Image delIcon = new Image(deletefile.toURI().toString());



        ImageView icon0 = new ImageView(resIcon);
        ImageView icon1 = new ImageView(paseIcon);
        ImageView icon2 = new ImageView(delIcon);
        icon0.setFitWidth(10);
        icon0.setFitHeight(10);
        icon1.setFitWidth(10);
        icon1.setFitHeight(10);

        icon2.setFitWidth(10);
        icon2.setFitHeight(10);


         pause  = new Button(null,icon1);
         resume  = new Button(null,icon0);
         delete= new Button(null,icon2);

        pause.setLayoutX(270);
        resume.setLayoutX(300);
        delete.setLayoutX(330);

        pause.setLayoutY(120);
        resume.setLayoutY(120);
        delete.setLayoutY(120);

        pause.setOnAction(e->{
            thread.suspend();
        });

        resume.setOnAction(e->{
            thread.resume();
        });

        ProgressBar pb = new ProgressBar(0.0);
        ProgressIndicator pi = new ProgressIndicator(0.0);
        Label label = new Label();
        label.setLayoutX(100);
        label.setLayoutY(80);
        label.setText(ob.getpath());
        pi.setLayoutX(510);
        pi.setLayoutY(80);
        pi.setPrefSize(50, 50);
        pb.setLayoutX(100);
        pb.setLayoutY(100);
        pb.setPrefWidth(400);
        pb.setProgress(0);
        pb.isIndeterminate();
        pb.progressProperty().unbind();
    pi.progressProperty().bind(ob.progressProperty());
        pb.progressProperty().bind(ob.progressProperty());
        Pane pane1 = new Pane();
    pane1.setLayoutY(40);
        pane1.getChildren().addAll(pb,pi,label,pause,resume,delete);
    return pane1;
    }



}