gpt4 book ai didi

java - 如何在 JScrollPane 中启用滚动?

转载 作者:行者123 更新时间:2023-12-02 02:56:57 24 4
gpt4 key购买 nike

我正在构建一个 View ,然后我意识到我需要在里面放入太多信息,因此它不适合窗口。因此,我决定创建一个 JScrollPane 将所有元素放入其中,并在需要时继续包含新元素,以便能够在我的窗口中看到所有内容。

这是我的滚动 Pane 的代码:

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 in runtime]

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

最佳答案

我遵循了一些建议,尝试使用布局,但这些都不适合我。最后,在 friend 的建议下,我成功做到了,没有布局,只是修改了我的旧方法。

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());
}

Scroll working now

关于java - 如何在 JScrollPane 中启用滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60903220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com