JTextarea具有动态文本长度和BoxLayout包装错误的高度

时间:2011-08-29 12:09:03

标签: height jtextarea stretch boxlayout

我正在尝试在一个veritcal BoxLayout中使用多行标签和图像标签。对于多行标签,我使用带有setEditable(false)的JTextArea。对于图像标签,我使用JLabel([ImageIcon])。

以下代码显示textarea下面有很多空间,我不希望这样。为了简单起见,我添加了文字标签而不是图片标签。

我想要的是从上到下堆叠textarea和标签。在每个文本区域之后,标签应紧跟在最后一个标签之后,在窗口底部应该有空的空间。

也许另一个布局管理器更好,但我认为这是一个JTextArea问题。任何解决方案都会有所帮助。

感谢。

这是可编辑的代码:

import java.awt.Color;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class BoxLay extends JFrame
{
private static final long serialVersionUID = 1L;

public static void main(final String[] args)
{
    new BoxLay();

}

private BoxLay()
{
    setTitle("BoxLayout TestDummy");
    setSize(800, 450);
    setResizable(true);
    setVisible(true);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));

    final JTextArea area1 = new JTextArea();
    area1.setText("First Text - Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... ");
    area1.setLineWrap(true);
    area1.setWrapStyleWord(true);
    area1.setEditable(false);
    area1.setBackground(Color.RED);
    this.add(area1);

    final JLabel label1 = new JLabel("DIRECTLY BELOW FIRST TEXT");
    this.add(label1);

    final JTextArea area2 = new JTextArea();
    area2.setText("Second Text - Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... ");
    area2.setLineWrap(true);
    area2.setWrapStyleWord(true);
    area2.setEditable(false);
    area2.setBackground(Color.RED);
    this.add(area2);

    final JLabel label2 = new JLabel("DIRECTLY BELOW SECOND TEXT");
    this.add(label2);

    this.add(Box.createVerticalGlue());

    this.getContentPane().invalidate();
    this.getContentPane().validate();

}
}

1 个答案:

答案 0 :(得分:0)

最后写了我自己的LayoutManager(dejavu!)。它只是从上到下堆叠组件,并不试图使它们适合整个屏幕。不过,这是一个原型,并且不支持X_AXIS。可能会有很多改进,但也许它可以帮助某人。我也很高兴,如果有人可以改进它并与我分享,因为我仍然是关于LayoutManagers的初学者。

无论如何,这是代码:

public class StackLayout implements LayoutManager

{

static final int  X_AXIS = 0;
static final int  Y_AXIS = 1;

private final int axis   = Y_AXIS;

public void addLayoutComponent(final String name, final Component comp)
{

}

public void removeLayoutComponent(final Component comp)
{

}

public Dimension preferredLayoutSize(final Container parent)
{

    return new Dimension(200, 200);

}

public Dimension minimumLayoutSize(final Container parent)
{

    return new Dimension(0, 0);

}

public void layoutContainer(final Container parent)
{
    //TODO implement x axis layout

    final Dimension size = parent.getSize();
    if (axis == Y_AXIS)
    {

        final int left = 0;

        int top = 0;

        for (int i = 0; i < parent.getComponentCount(); ++i)
        {
            final Component component = parent.getComponent(i);

            component.setSize(parent.getSize().width, 1);

            final Dimension preferredSize = component.getPreferredSize();

            component.setBounds(left, top, Math.max(preferredSize.width, parent.getSize().width), preferredSize.height);

            top += preferredSize.height;
        }
        parent.setPreferredSize(new Dimension(parent.getSize().width, top));
    }

}

}