简体   繁体   English

如何使可滚动到jPanel

[英]How to make scrollable to jPanel

I am making swing application. 我正在制作摇摆应用程序。 And there is too much height of my jPanel. 我的jPanel的高度太高了。 So I want to make this panel as scrollable.: Following is my description of my requirement. 所以我想让这个面板可滚动。以下是我对我的要求的描述。

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: 我在p1添加了p2, p3, p4 ,如下面的输出:

MyOutput中

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. 但是,答案并没有解决我的问题。

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. 由于您使用NetBeans, JScrollpane从调色板中添加一个JScrollpane ,您将在其中添加一个面板以包含其他4个面板。 I think you could also just add the 4 panel into the JScrollpane . 我想你也可以将4面板添加到JScrollpane

Add your panel to a JScrollPane . 将面板添加到JScrollPane Assumed that you want vertical scrolling only: 假设您只想要垂直滚动:

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. 要微调滚动量,您可以选择实现Scrollable接口。
See also How to Use Scroll Panes (The Java Tutorial) 另请参见如何使用滚动窗格(Java教程)

It is easy to design scroll pane using Netbeans IDE. 使用Netbeans IDE很容易设计滚动窗格。 Below given are the steps I followed to add a scroll pane: 下面给出了我添加滚动窗格所遵循的步骤:

    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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM