import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TextExample{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setTitle("Ein Titel");
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Hier würde ich lieber die Breite angeben und die Höhe berechnen. Leider finde ich keine Methode zum Ermitteln der tatsächlichen Höhe eines JLabels mit Zeilenumbruch, wenn ich nicht zuerst das JFrame sichtbar mache
frame.setPreferredSize(new Dimension(300, 300));
JLabel label1 = new JLabel("<html><p align = 'justify'>Das ist ein so langer Text, dass er ohne Zeilenumbruch einfach nicht auskommt. Außerdem will er unbedingt im Blocksatz stehen, damit er nicht so hingerotzt wirkt. Dabei soll er seine Breite an die vorgegebene anpassen und seine Höhe so ändern, dass gerade noch der ganze Text zu sehen ist.</p></html>");
JLabel label2 = new JLabel("Eine Beschriftung:");
JTextField textfield1 = new JTextField("Eine Eingabe");
JLabel label3 = new JLabel("Noch 'ne Beschriftung");
JTextField textfield2 = new JTextField("Noch 'ne Eingabe");
JCheckBox checkbox = new JCheckBox("Eine Checkbox", true);
JButton button1 = new JButton("Ein Button");
JButton button2 = new JButton("Noch ein Button");
Container content = frame.getContentPane();
Insets insets = new Insets(3, 3, 3, 3);
frame.setLayout(new GridBagLayout());
content.add(label1, new GridBagConstraints(0, 0, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
content.add(label2, new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
content.add(textfield1, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
content.add(label3, new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
content.add(textfield2, new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
content.add(checkbox, new GridBagConstraints(0, 3, 2, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
content.add(button1, new GridBagConstraints(0, 4, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
content.add(button2, new GridBagConstraints(1, 4, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
frame.pack();
frame.setVisible(true);
}
}