简体   繁体   English

NetBeans:如何将ScrollBar添加到JPanel

[英]NetBeans: how to add ScrollBar to JPanel

I am developing a small desktop application in NetBeans. 我正在NetBeans中开发一个小型桌面应用程序。 On my UI, I place a JPanel and put a single JLabel on it. 在我的UI上,我放置一个JPanel并在其上放置一个JLabel。 The content of this JLabel is generated dynamically, so if the content is very large then it goes out of screen.So is there any way in which I can specify a fixed size for the JPanel and of course ScrollBars should appear when the text exceeds the screen size. 这个JLabel的内容是动态生成的,所以如果内容非常大,那么它会离开屏幕。所以我可以用任何方式为JPanel指定一个固定的大小,当然当文本超出时,ScrollBars会出现屏幕尺寸。

You will have to just pass Component reference to the JScrollPane Constructor. 您必须将Component引用传递给JScrollPane构造函数。 It will work fine. 它会工作正常。 You can definetely use JScrollPane The following is the sudo example of the JScrollPane for JPanel from my past project. 您可以定义使用JScrollPane以下是我过去的项目中JPanel的JScrollPane的sudo示例。 Hope it will be useful for you. 希望它对你有用。

import javax.swing.*;
import java.awt.*;

public class Frame01
{
    public static void main(String[] args){
        SwingUtilities.invokeLater (new Runnable ()
        {
            public void run ()
            {
                JFrame frame = new JFrame("panel demo");
                frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);

                JPanel panel = new JPanel();
                Container c = frame.getContentPane();
                panel.setSize(100,100);
                panel.setLayout(new GridLayout(1000,1));
                for(int i = 0; i<1000;i++)
                panel.add(new JLabel("JLabel "+i));

                JScrollPane jsp = new JScrollPane(panel);
                c.add(jsp);
                frame.setSize(100,100);
                frame.setVisible(true);
            }
        });
    }
}

使用JScrollPane包含您的大JPanel。

so in case when the contents are very large it goes out of screen ,也许您必须将TextComponents作为JTextArea或JEditorPane来查找,教程包含的示例包括JScrollPane的基本用法

In the Navigator, click on JPanel with the right mouse button --> Enclose In --> Scroll Pane. 在导航器中,使用鼠标右键单击JPanel - > Enclose In - > Scroll Pane。

Done!, You have now scrolls 完成!,你现在有了滚动

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

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