如何在Swing中直接将滚动条添加到JFrame?

时间:2021-12-23 23:17:17

I know there are various methods to add Scroll to JPanel then add the panel to a JFrame.

我知道有多种方法可以将Scroll添加到JPanel,然后将面板添加到JFrame。

But when I tried to add JScrollPane to the frame directly without using a panel, that was an error:

但是当我尝试直接将JScrollPane添加到框架而不使用面板时,这是一个错误:

window can not be inside the container

So in my code, I need to add Scroll directly to my frame.

所以在我的代码中,我需要将Scroll直接添加到我的框架中。

1 个解决方案

#1


1  

Maybe this example will help:

也许这个例子会有所帮助:

JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
JFrame frame = new JFrame();
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(pnl, BorderLayout.PAGE_END);`

#1


1  

Maybe this example will help:

也许这个例子会有所帮助:

JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
JFrame frame = new JFrame();
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(pnl, BorderLayout.PAGE_END);`