JTextArea 中的换行导致 JScrollPane 与 MiGLayout 表现不佳

发布于 2024-11-08 03:32:15 字数 1134 浏览 1 评论 0原文

我遇到了和这个人同样的问题:

与 linewrap=true 一起使用时,MigLayout JTextArea 不会缩小

,我使用了答案之一中描述的解决方案;明确设置最小尺寸。如果将包含 JTextArea 的 JPanel 直接放置在 JFrame 中,然后调整窗口大小,则效果很好。

但是,当将包含 JTextArea 的面板放置在 JScrollPane 中时, 同样的问题再次出现。这是为什么?如何解决?

干杯

编辑:一个例子

public class MiGTest2 extends JFrame{   
public MiGTest2(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));
    JTextArea textArea  = new JTextArea();
    textArea.setLineWrap(true);
    panel.add(textArea, "wmin 10");
    //panel.add(new JTextField());
    JScrollPane scrollPane = new JScrollPane(panel);
    //add(panel);
    add(scrollPane);
    pack();
}
public static void main(String[] args){
    new MiGTest2().setVisible(true);
}
}

如果您取消注释//add(panel);,并注释add(scrollPane);,缩小窗口大小将还缩小了 JTextArea。也就是说,它不适用于 JScrollPane。另请注意,在第一次放大窗口后缩小窗口大小时,布局管理器似乎会翻转并开始“摇动”其所有内容

I am having trouble with the same thing as this guy:

MigLayout JTextArea is not shrinking when used with linewrap=true

and I used the solution described in one of the answers; to set the minimum size explicitly. This works fine if one places the JPanel which contains the JTextArea directly in a JFrame, and then resizes the window.

However, when placing the panel which contains the JTextArea inside a JScrollPane,
the same problem occurs again. Why is this, and how can one fix it?

Cheers

EDIT: An example

public class MiGTest2 extends JFrame{   
public MiGTest2(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));
    JTextArea textArea  = new JTextArea();
    textArea.setLineWrap(true);
    panel.add(textArea, "wmin 10");
    //panel.add(new JTextField());
    JScrollPane scrollPane = new JScrollPane(panel);
    //add(panel);
    add(scrollPane);
    pack();
}
public static void main(String[] args){
    new MiGTest2().setVisible(true);
}
}

If you uncomment //add(panel);, and comment add(scrollPane);, shrinking the window size will also shrink the JTextArea. That is, it does not work with a JScrollPane. Also note how the layout manager seems to flip out and starts "shaking" all its contents when shrinking the size of the window after first enlarging it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柳若烟 2024-11-15 03:32:16

我有一个非常类似的问题,并遵循上述 问题也没有帮助我。然而,它确实提供了一个有价值的想法——问题在于启用换行的 JTextArea 的宽度。

对我有用的是使用命令 width 在组件级别设置最小宽度和首选宽度。例如,宽度 10:500:

I had a very similar problem and following the answer in the mentioned question did not help me either. However, it did provide a valuable idea -- the problem is in the width of the JTextArea with wrap enabled.

What worked for me was setting both minimum and preferred width at the component level using command width. For example, width 10:500:.

眼角的笑意。 2024-11-15 03:32:16

当与 JScrollPanes 一起使用时,我在 JTextAreas 和换行方面也遇到了类似的问题。

对我有用的解决方案是创建一个自定义面板,该面板实现 Scrollable 接口并重写 getScrollableTracksViewportWidth() 方法以返回 true。这会导致滚动窗格仅垂直滚动,并让 JTextArea 中的换行按预期工作。

/**
 * A panel that, when placed in a {@link JScrollPane}, only scrolls vertically and resizes horizontally as needed.
 */
public class OnlyVerticalScrollPanel extends JPanel implements Scrollable
{
    public OnlyVerticalScrollPanel()
    {
        this(new GridLayout(0, 1));
    }

    public OnlyVerticalScrollPanel(LayoutManager lm)
    {
        super(lm);
    }

    public OnlyVerticalScrollPanel(Component comp)
    {
        this();
        add(comp);
    }

    @Override
    public Dimension getPreferredScrollableViewportSize()
    {
        return(getPreferredSize());
    }

    @Override
    public int getScrollableUnitIncrement(Rectangle visibleRect,
            int orientation, int direction)
    {
        return(10);
    }

    @Override
    public int getScrollableBlockIncrement(Rectangle visibleRect,
            int orientation, int direction)
    {
        return(100);
    }

    @Override
    public boolean getScrollableTracksViewportWidth()
    {
        return(true);
    }

    @Override
    public boolean getScrollableTracksViewportHeight()
    {
        return(false);
    }
}

MigTest2 变为:

public class MiGTest2 extends JFrame
{   
    public MiGTest2()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));
        JTextArea textArea  = new JTextArea();
        textArea.setLineWrap(true);
        panel.add(textArea, "wmin 10");
        //panel.add(new JTextField());

        //Wrap panel with the OnlyVerticalScrollPane to prevent horizontal scrolling
        JScrollPane scrollPane = new JScrollPane(new OnlyVerticalScrollPanel(panel));
        //add(panel);
        add(scrollPane);
        pack();
    }

    public static void main(String[] args)
    {
        new MiGTest2().setVisible(true);
    }
}

I've had similar problems with JTextAreas and wrapping when used with JScrollPanes.

A solution that worked for me was to create a custom panel that implements the Scrollable interface and overrides the getScrollableTracksViewportWidth() method to return true. This forces causes the scroll pane to only scroll vertically and lets line wrapping in the JTextArea work as expected.

/**
 * A panel that, when placed in a {@link JScrollPane}, only scrolls vertically and resizes horizontally as needed.
 */
public class OnlyVerticalScrollPanel extends JPanel implements Scrollable
{
    public OnlyVerticalScrollPanel()
    {
        this(new GridLayout(0, 1));
    }

    public OnlyVerticalScrollPanel(LayoutManager lm)
    {
        super(lm);
    }

    public OnlyVerticalScrollPanel(Component comp)
    {
        this();
        add(comp);
    }

    @Override
    public Dimension getPreferredScrollableViewportSize()
    {
        return(getPreferredSize());
    }

    @Override
    public int getScrollableUnitIncrement(Rectangle visibleRect,
            int orientation, int direction)
    {
        return(10);
    }

    @Override
    public int getScrollableBlockIncrement(Rectangle visibleRect,
            int orientation, int direction)
    {
        return(100);
    }

    @Override
    public boolean getScrollableTracksViewportWidth()
    {
        return(true);
    }

    @Override
    public boolean getScrollableTracksViewportHeight()
    {
        return(false);
    }
}

and MigTest2 becomes:

public class MiGTest2 extends JFrame
{   
    public MiGTest2()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));
        JTextArea textArea  = new JTextArea();
        textArea.setLineWrap(true);
        panel.add(textArea, "wmin 10");
        //panel.add(new JTextField());

        //Wrap panel with the OnlyVerticalScrollPane to prevent horizontal scrolling
        JScrollPane scrollPane = new JScrollPane(new OnlyVerticalScrollPanel(panel));
        //add(panel);
        add(scrollPane);
        pack();
    }

    public static void main(String[] args)
    {
        new MiGTest2().setVisible(true);
    }
}
枕花眠 2024-11-15 03:32:16

通常,您会将 JTextArea 放入 JScrollPane 中。像这样:

JTextArea area = new JTextArea();
JScrollPane scroll = new JScrollPane(area);
JPanel panel = new JPanel();
panel.add(scroll);

Normally, you would put the JTextArea into your JScrollPane. Like this:

JTextArea area = new JTextArea();
JScrollPane scroll = new JScrollPane(area);
JPanel panel = new JPanel();
panel.add(scroll);
静谧幽蓝 2024-11-15 03:32:16

不太确定您想在这里实现什么,请尝试运行它并看看它是否适合您的需求?


public class MiGTest2 extends JFrame {
    public MiGTest2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));

        JTextArea textArea = new JTextArea();
        textArea.setLineWrap(true);
        panel.add(new JScrollPane(textArea), "wmin 10, grow, push");

        setLayout(new MigLayout("fill"));

        JScrollPane scrollPane = new JScrollPane(panel);
        add(scrollPane, "grow, push");

        pack();
    }

    public static void main(String[] args) {
        new MiGTest2().setVisible(true);
    }
}

Not really sure what you're trying to achieve here, try running this and see if it fits your need?


public class MiGTest2 extends JFrame {
    public MiGTest2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));

        JTextArea textArea = new JTextArea();
        textArea.setLineWrap(true);
        panel.add(new JScrollPane(textArea), "wmin 10, grow, push");

        setLayout(new MigLayout("fill"));

        JScrollPane scrollPane = new JScrollPane(panel);
        add(scrollPane, "grow, push");

        pack();
    }

    public static void main(String[] args) {
        new MiGTest2().setVisible(true);
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文