如何在 JScrollPane 中启用滚动?

Dai*_*vas 2 java swing jscrollpane

我正在构建一个视图,然后我意识到我需要在里面放太多信息,所以它不适合窗口。所以我决定创建一个 JScrollPane 来把所有元素放在里面,如果需要,继续包含新元素,能够在我的窗口中看到它。

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

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;
}
Run Code Online (Sandbox Code Playgroud)

但是当我编译时,我只能看到这个:

[运行时的 JScrollPane]

如您所见,未显示滚动条。过去我没有经常使用 JScrollPane,也许我缺少一些启用滚动的属性?

Dai*_*vas 5

我遵循了几个建议,尝试使用布局,但这些都不适合我。最后,感谢朋友的建议,我设法做到了,没有布局,只是修改了我的旧方法。

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());
}
Run Code Online (Sandbox Code Playgroud)

滚动现在工作