Java 如何使 jPanel 可滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18408668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make scrollable to jPanel
提问by Yubaraj
I am making swing application. And there is too much height of my jPanel. So I want to make this panel as scrollable.: Following is my description of my requirement.
我正在制作摇摆应用程序。而且我的 jPanel 的高度太多了。所以我想让这个面板可以滚动。:以下是我对我的要求的描述。
I have four jpanel in one jpanel I mean:
我在一个 jpanel 中有四个 jpanel 我的意思是:
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
I am adding p2, p3, p4
inside p1
like following output:
我在p2, p3, p4
里面添加p1
如下输出:
like above showing panel has more height than computer screen height. So I want to display all content of my panel on computer screen by scrolling.
如上所示,显示面板的高度高于计算机屏幕的高度。所以我想通过滚动在计算机屏幕上显示我的面板的所有内容。
I searched here and found the following questions:
我在这里搜索并发现以下问题:
However, the answers did not solve myproblem.
但是,答案并没有解决我的问题。
采纳答案by Jonathan Drapeau
Without seeing your code, my guess is that you don't have a JScrollpane
to provide the scrollable behaviour you want.
没有看到您的代码,我的猜测是您没有JScrollpane
提供所需的可滚动行为。
JPanel mainPanel = new JPanel(); //This would be the base panel of your UI
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel newPanel = new JPanel();
newPanel.add(p1);
newPanel.add(p2);
newPanel.add(p3);
newPanel.add(p4);
JScrollPane pane = new JScrollPane(newPanel);
mainPanel.add(pane);
Since you use NetBeans, add a JScrollpane
from the palette in which you'll add a panel to contain the 4 others. I think you could also just add the 4 panel into the JScrollpane
.
由于您使用 NetBeans,因此请JScrollpane
从调色板中添加一个,您将在其中添加一个面板以包含其他 4 个面板。我认为您也可以将 4 面板添加到JScrollpane
.
回答by Peter Walser
Add your panel to a JScrollPane
. Assumed that you want vertical scrolling only:
将您的面板添加到JScrollPane
. 假设您只想垂直滚动:
JScrollPane scrollPane=new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
For fine-tuning the scroll amounts, you can optionally implement the Scrollable
interface.
See also How to Use Scroll Panes (The Java Tutorial)
为了微调滚动量,您可以选择实现该Scrollable
接口。
另请参阅如何使用滚动窗格(Java 教程)
回答by Renjith Thomas
It is easy to design scroll pane using Netbeans IDE. Below given are the steps I followed to add a scroll pane:
使用 Netbeans IDE 设计滚动窗格很容易。下面给出的是我添加滚动窗格所遵循的步骤:
1. In Netbeans GUI editor, select all panels which requires scroll pane using CTRL+left click
2. Right click on the hilighted panels, select the option 'Enclose in' -> Scroll Pane. This will add a scroll pane for the selected panels.
3. If there are other elements than Panel(say JTree), select all the elements ->Enclose in ->Panel. Then enlose the new parent panel to scroll pane
4. Make sure that 'Auto Resizing' is turned on for the selected parent panel(Right click on panel -> Auto resizing -> Tick both Horizontal and vertical)