5

我和这个人有同样的问题:

与 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。还要注意布局管理器在第一次放大后缩小窗口大小时似乎是如何翻转并开始“摇晃”其所有内容的

4

4 回答 4

9

我有一个非常相似的问题,并且按照上述问题中的答案对我也没有帮助。然而,它确实提供了一个有价值的想法——问题在于启用了换行的 JTextArea 的宽度。

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

于 2011-10-20T08:59:19.357 回答
7

当与 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);
    }
}
于 2011-11-15T01:42:07.203 回答
0

不太确定您要在这里实现什么,尝试运行它,看看它是否符合您的需要?


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);
    }
}

于 2011-10-24T08:59:33.047 回答
0

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

JTextArea area = new JTextArea();
JScrollPane scroll = new JScrollPane(area);
JPanel panel = new JPanel();
panel.add(scroll);
于 2011-05-16T20:52:11.923 回答