5

我正在 NetBeans 中开发一个小型桌面应用程序。在我的 UI 上,我放置了一个 JPanel 并在其上放置了一个 JLabel。这个 JLabel 的内容是动态生成的,所以如果内容很大,那么它就会超出屏幕。那么有什么方法可以为 JPanel 指定固定大小,当然当文本超过屏幕尺寸。

4

4 回答 4

8

您只需将 Component 引用传递给 JScrollPane 构造函数。它会正常工作。您可以定义使用 JScrollPane 以下是我过去项目中 JScrollPane for JPanel 的 sudo 示例。希望它对你有用。

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);
            }
        });
    }
}
于 2011-10-01T11:52:43.197 回答
6

使用 JScrollPane 包含您的大 JPanel。

于 2011-10-01T11:27:18.577 回答
2

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

于 2011-10-01T11:33:06.150 回答
2

在 Navigator 中,用鼠标右键单击 JPanel --> Enclose In --> Scroll Pane。

完成!,你现在有卷轴

于 2013-12-21T09:30:59.213 回答