我正在构建一个 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,也许我缺少一些启用滚动的属性?
最佳答案
我遵循了一些建议,尝试使用布局,但这些都不适合我。最后,在 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());
}
关于java - 如何在 JScrollPane 中启用滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60903220/