JTextarea 具有动态文本长度并在 BoxLayout 中换行错误高度
我正在尝试在 veritcal BoxLayout 中使用多行标签和图像标签。对于多行标签,我使用带有 setEditable(false) 的 JTextArea。对于图像标签,我使用 JLabel([ImageIcon])。
下面的代码显示文本区域下面有很多空间,但我不希望这样。为了简单起见,我添加了文本标签而不是图像标签。
我想要的是将文本区域和标签从上到下堆叠。在每个文本区域之后,标签应紧随其后,在最后一个标签之后,应有空白区域直至窗口底部。
也许另一个布局管理器更好,但我认为这是一个 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();
}
}
I'm trying to have multiline labels and image labels in a veritcal BoxLayout. For the multiline labels I use a JTextArea with setEditable(false). For the image labels I use a JLabel([ImageIcon]).
The following code shows that the textarea has a lot of space below it and I don't want that. To keep it simple I added text labels instead of image labels.
What I want is to stack the textarea and the labels from top to bottom. After each textarea the label should follow immediately below and after the last label there should be empty space up to the bottom of the window.
Maybe another Layout Manager is better, but I think it is a JTextArea issue. Any solution would help.
Thanks.
here is the compilable code:
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后写了我自己的LayoutManager(dejavu!)。它只是从上到下堆叠组件,并不试图让它们适合整个屏幕。不过,它只是一个原型,并且不支持 X_AXIS。可能还有很多可以改进的地方,但也许它对某人有帮助。如果有人可以改进它并与我分享,我也会非常高兴,因为我仍然是布局管理器的初学者。
无论如何,这是代码:
{
}
Finally wrote my own LayoutManager (dejavu!). It simply stacks components from top to bottom and does not try to make them fit the whole screen. It's a prototype, though, and X_AXIS is not supported. Probably a lot can be improved, but maybe it helps someone. I would also be very happy, if someone can improve it an share it with me, because I'm still a beginner regarding LayoutManagers.
Anyway, here is the code:
{
}