如何给 Java Swing JScrollPane 添加边框?

How to add a border to a Java Swing JScrollPane?

我试过使用 setBorder() 方法向 JScrollPane 添加边框,但没有成功。如何为 JScrollPane 添加边框?

根据 JScrollPane documentation,您可以使用 setViewportBorder 在主视口周围添加边框。

scrollpane.setViewportBorder(BorderFactory.createLineBorder(Color.black));

或者您可以使用 setBorder 在整个滚动窗格周围添加边框,如 this Oracle tutorial:

中所述
pane.setBorder(BorderFactory.createLineBorder(Color.black));