java - JTextArea 不会使用布局管理器收缩到 JPanel 内

标签 java swing jscrollpane jtextarea layout-manager

我有一个自定义布局,其中主要行为是当 JScrollPane 宽度发生变化时增大和缩小子 JTextArea 。滚动 Pane 禁用水平滚动条,并且文本区域应该扩展和收缩以避免需要水平滚动条。几个月来,我使用一个标准布局管理器解决了这个问题,但现在我需要一些不同的功能。

发生的情况是,当用户水平展开滚动 Pane 时,将调用布局管理器的layoutContainer 方法。它会调整文本区域的大小并且文本会正确重排。但是,当您缩小滚动 Pane 时,不会调用layoutContainer,并且文本区域保持固定。我在layoutContainer 方法中放置了一些println,以使其在工作时和不工作时显而易见。

需要注意的重要一点是,当调用 JTextArea.setColumns() 时会出现问题。我可以将其注释掉,并且在调整大小期间调用layoutContainer(当然,然后文本区域不会调整大小。)我也尝试过使用JTextArea.setSize(),得到相同的结果.

代码如下:

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
@SuppressWarnings("serial")
class XTextArea extends JTextArea
{
    XTextArea (String text)
    {
        super (text);
    }

    public int getColumnWidth()
    {
        return super.getColumnWidth();
    }
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
class PackLeftLayout implements LayoutManager
{
    Component viewPort;
    Component flexWidthComponent;

    int preferredWidth = 0;
    int preferredHeight = 0;

    //----------------------------------------------------------------------------
    // viewPort - if null, compute width as sum of component's preferred width;
    //  otherwise width will be the viewPort's width.
    // flexWidthComponent - if not null, this component width will be sized to right
    //   justify rightmost component.
    public PackLeftLayout (Component viewPort, Component flexWidthComponent)
    {
        super ();
        this.viewPort = viewPort;
        this.flexWidthComponent = flexWidthComponent;
    }

    //----------------------------------------------------------------------------
    public void addLayoutComponent(String name, Component comp)
    {
    }

    //----------------------------------------------------------------------------
    public void removeLayoutComponent(Component comp)
    {
    }

    //----------------------------------------------------------------------------
    // Calculates the preferred size dimensions for the specified container, given the
    // components it contains.
    // parent - the container to be laid out
    // Components layed out left-to-right with no additional spacing.
    public Dimension preferredLayoutSize (Container parent)
    {
        Insets insets = parent.getInsets();
        int width = 0;
        int height = 0; // will become max of all component's preferred height

        // calculate sum of fixed width components - skip the flexwidth component
        width = insets.left + insets.right;
        for (int i = 0, limit = parent.getComponentCount();  i < limit;  i++)
        {
            Component c = parent.getComponent(i);
            if (c.isVisible())
            {
                if (c != flexWidthComponent)
                {
                    Dimension size = c.getPreferredSize();
                    if (size.height > height)
                    height = size.height;
                    width += size.width;
                }
            }
        }

        // determine width of flex width component
        if (flexWidthComponent != null)
        {
            int flexWidth = viewPort.getWidth() - width;
            if (flexWidth < 1)
                flexWidth = 1;

            if (flexWidthComponent instanceof XTextArea)
            {
                // some trickery here to get the xtextarea to tell us its preferred height
                // given a specific width.
                int colWidth = ((XTextArea)flexWidthComponent).getColumnWidth();

                // the following line causes the failure:
                ((XTextArea)flexWidthComponent).setColumns (flexWidth / colWidth);

                Dimension taSize = flexWidthComponent.getPreferredSize();
                width += taSize.width;

                if (taSize.height > height)
                    height = taSize.height;
            }
            else
            {
                Dimension size = flexWidthComponent.getPreferredSize();
                width += flexWidth;
                if (size.height > height)
                    height = size.height;
            }
        }

        preferredWidth = width; // already include insets
        preferredHeight = height + insets.top + insets.bottom;

        return new Dimension (preferredWidth, preferredHeight);
    }

    //----------------------------------------------------------------------------
    // Calculates the minimum size dimensions for the specified container, given the
    // components it contains.
    // parent - the component to be laid out
    public Dimension minimumLayoutSize(Container parent)
    {
      return new Dimension (10, 10); //???
    }

    static int k = 0;
    //----------------------------------------------------------------------------
    public void layoutContainer(Container parent)
    {
        System.out.println ("layout" + (k++));
        Insets insets = parent.getInsets();
        int left = insets.left;

        if (preferredWidth == 0 || preferredHeight == 0)
            preferredLayoutSize (parent);

        for (int i = 0, limit = parent.getComponentCount();  i < limit;  i++)
        {
            Component c = parent.getComponent(i);
            Dimension size = c.getPreferredSize();

            c.setBounds (left, insets.top, size.width, preferredHeight);

            left += size.width;
        }

        // force another layout calc
        preferredWidth = 0;
    }
}

public class ResizablePane extends JFrame
{

    public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater(
            new Runnable() {public void run()
            {
                new ResizablePane();
            }});
    }

    ResizablePane ()
    {
        super ("ResizableDemo");

        // put a button and text area into a panel, then into a scroll pane
        JButton button = new JButton ("button");
        XTextArea text = new XTextArea (
            "For three years I ran as fast as I could, trying to live and love and learn at " +
            "double speed to make up for what Anne-Marie lost.  Trying to anesthetize myself " +
            "from what Id lost.  When I decided to read a book a day and write about it, Id " +
            "finally stopped running away.");

        text.setLineWrap (true);
        text.setWrapStyleWord (true);

        JScrollPane scroll = new JScrollPane();
        JPanel panel = new JPanel();
        panel.setLayout (new PackLeftLayout(scroll.getViewport(), text));
        panel.add (button);
        panel.add (text);

        scroll.setHorizontalScrollBarPolicy (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.setViewportView (panel);

        getContentPane().add(scroll);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
}

最佳答案

JTextArea 不会在 JPanel 内收缩

您需要设置文本区域的最小尺寸:

textArea.SetMinimumSize(new Dimension(100,100));

ps。我在面板中使用 GridLayout,仅使用一个组件。

关于java - JTextArea 不会使用布局管理器收缩到 JPanel 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7960939/

相关文章:

java - 如何同步传递到多个地方的实例?

java - 为 Java 8 上的 Tomcat 8.5 分配更多内存

Nodejs 中的 Java 密码

java - 在 Visual Studio Code 中部署 Azure Function 不包含 .jar 文件

java - JPanel/Graphics 方法paintComponent 是什么?

java - JTextPane 拒绝对长字符串进行自动换行

java - JScrollPane 自动换行

java - 在 JTextArea 中突出显示一个特定的行/行

java - 设置面板的背景颜色在 JTabbedPane 外部有效,但在 JTabbedPane 内部无效

java - JScrollPane:重新验证后滚动位置