縦型 BoxLayout に複数行のラベルと画像ラベルを入れようとしています。複数行のラベルについては、setEditable(false) で JTextArea を使用します。画像ラベルには JLabel([ImageIcon]) を使用します。
次のコードは、テキストエリアの下に多くのスペースがあることを示していますが、それは望ましくありません。シンプルにするために、画像ラベルの代わりにテキスト ラベルを追加しました。
私が欲しいのは、テキストエリアとラベルを上から下に積み重ねることです。各テキストエリアの直後にラベルが続き、最後のラベルの後にはウィンドウの下部まで空のスペースが必要です。
別の Layout Manager の方が良いかもしれませんが、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();
}
}