Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTextArea中的换行会导致JScrollPane与MiGLayout行为不当_Java_Swing_Jscrollpane_Jtextarea_Miglayout - Fatal编程技术网

Java JTextArea中的换行会导致JScrollPane与MiGLayout行为不当

Java JTextArea中的换行会导致JScrollPane与MiGLayout行为不当,java,swing,jscrollpane,jtextarea,miglayout,Java,Swing,Jscrollpane,Jtextarea,Miglayout,我和这个家伙有同样的问题: 我使用了其中一个答案中描述的解决方案;要显式设置最小大小。如果将包含JTextArea的JPanel直接放在JFrame中,然后调整窗口的大小,这种方法就可以了 但是,当将包含JTextArea的面板放置在JScrollPane中时, 同样的问题再次出现。这是为什么?人们如何解决它 干杯 编辑:一个示例 public class MiGTest2 extends JFrame{ public MiGTest2(){ setDefaultCloseOpe

我和这个家伙有同样的问题:

我使用了其中一个答案中描述的解决方案;要显式设置最小大小。如果将包含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);
}
}

如果取消注释
//添加(面板)和注释
添加(滚动窗格),缩小窗口大小也会缩小JTextArea。也就是说,它不适用于JScrollPane。还要注意布局管理器在第一次放大窗口后缩小窗口大小时是如何翻转并开始“摇晃”所有内容的。通常,您会将JTextArea放入JScrollPane中。像这样:

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

我有一个非常类似的问题,按照上面提到的答案也没有帮助我。然而,它确实提供了一个有价值的想法——问题在于启用了wrap的JTextArea的宽度


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

不确定您在这里想要实现什么,请尝试运行此程序,看看它是否适合您的需要


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

当与JScrollPanes一起使用时,我在JTextAreas和包装方面也遇到过类似的问题

一个对我有效的解决方案是创建一个自定义面板,该面板实现可滚动接口,并重写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);
    }
}

这里描述了JPanels定义的所有可能的构造函数,除了构造函数之外,上面的例子不适用。或者你可以编辑上面的例子,让它工作?到目前为止,我还没有找到一个组合works@mKorbel妈的,我快把它寄出去了。更正了,抱歉。嗯,这不是(克利奥帕特拉的)@Korbel先生对不起,我不知道你指的是什么?谢谢prunge,但那根本不起作用。我在一个面板中得到一个JTextArea(因此它被限制在一行),当框架变小时,它不会收缩,它只是在一个面板中得到一个水平滚动条。你需要将wmin设置为约束,否则它不起作用。我只是用了这个,效果很好。这实际上是答案的核心-让MigLayout来管理大小-通过一个扭曲,所有的JTextArea都需要在框架上考虑。我有一个案例,其中一个没有说明(很难说,因为它基本上是一个标签),这导致面板上的所有JTextArea都失败了。