如何在 JScrollPane 中启用滚动?

How can I enable the scrolling in a JScrollPane?

我正在构建一个视图,然后我意识到我需要在里面放太多信息,所以 window 放不下。所以我决定创建一个 JScrollPane 将所有元素放入其中,并在需要时继续包含新元素,以便能够在我的 window.

中看到所有内容

这是我的滚动窗格的代码:

public JPanel getActionsPane() {
    if (Objects.isNull(actionsPane)){
        actionsPane = new JPanel();
        actionsPane.setLayout(null);
        actionsPane.setBounds(0, 29, 1580, 1450);
        addComponents();
    }
    return actionsPane;
}

public JScrollPane getActionsScrollPane() {
    if (Objects.isNull(actionsScrollPane)){
        actionsScrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        actionsScrollPane.add(getActionsPane());
        actionsScrollPane.setLayout(new ScrollPaneLayout());
        actionsScrollPane.setBounds(0, 29, 593, 400);
        actionsScrollPane.setViewportView(getActionsPane());
    }
    return actionsScrollPane;
}

但是我编译的时候只能看到这个:

[

如您所见,卷轴没有显示。过去我没有太多地使用 JScrollPane,也许我缺少一些启用卷轴的属性?

我遵循了一些建议,尝试使用布局,none 这对我有用。最后,感谢朋友的建议,我设法做到了,没有布局,只是修改了我的老方法。

public UI_ReembolsoFondoRegistrar(Window window) {
    super(window);
    setBounds(100, 100, 593, 750);
    setLocationRelativeTo(null);
    setTitle("Reembolsar el fondo para dietas y pagos menores");
    buttonPane.add(getBtnAceptar());
    buttonPane.add(getBtnCancelar());
    contentPanel.setLayout(null);
    contentPanel.add(getLblFecha());

    contentPanel.add(getActionsScrollPane());

    contentPanel.add(getLblCustodio());
    contentPanel.add(getCbxCustodio());
    contentPanel.add(getLblContab());
    contentPanel.add(getCbxContab());
    contentPanel.add(getLblRevisado());
    contentPanel.add(getCbxRevisado());
    contentPanel.add(getLblAprobado());
    contentPanel.add(getCbxAprobado());
    contentPanel.add(getLblCheque());
    contentPanel.add(getTxtCheque());
    contentPanel.add(getLblFecha_1());
    contentPanel.add(getDateChooser());
    contentPanel.add(getLblAnotado());
    contentPanel.add(getCbxAnotado());
    contentPanel.add(getSeparator_2());
}

public JPanel getActionsPane() {
    if (Objects.isNull(actionsPane)){
        actionsPane = new JPanel();
        actionsPane.setLayout(null);
        actionsPane.setPreferredSize(new Dimension(1580,525));
        addComponents();
    }
    return actionsPane;
}

public JScrollPane getActionsScrollPane() {
    if (Objects.isNull(actionsScrollPane)){
        actionsScrollPane = new JScrollPane(getActionsPane(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        actionsScrollPane.setViewportView(getActionsPane());
        actionsScrollPane.setBounds(0,28,593,480);
    }
    return actionsScrollPane;
}

private void addComponents(){
    actionsPane.add(getPanelGastosViaje());
    actionsPane.add(getPanelValePM());
    actionsPane.add(getLblCuenta());
    actionsPane.add(getCbxCuenta());
    actionsPane.add(getLblOCC());
    actionsPane.add(getCbxOCC());
    actionsPane.add(getLblCUP());
    actionsPane.add(getTxtCUP());
    actionsPane.add(getTxtTotalCUP());
    actionsPane.add(getBtnDesCUP());
    actionsPane.add(getSeparator_1());
    actionsPane.add(getLblCUC());
    actionsPane.add(getTxtCUC());
    actionsPane.add(getTxtTotalCUC());
    actionsPane.add(getBtnDesCUC());
    actionsPane.add(getContrapartidasTableJP());
}