Wie komme ich zu gewünschte Loginlayout?

Hallo Leute,

könnt ihr kurze Anweisung geben wie ich zu gewünschte Layout komme? Meine Schaut sehr komisch aus

public class Login extends JFrame {
	private JPanel loginPanel = new JPanel();
	private JLabel lUsername = new JLabel("Username");
	private JTextField tfUsername = new JTextField();
	private JLabel lPassword = new JLabel("Password");
	private JTextField tfPassword = new JTextField();
	private JButton bOK = new JButton("OK");
	private JButton bCancel = new JButton("Cancel");

	public static void main(String[] args) {
		Login login = new Login();
		login.setSize(400, 300);
		login.setLocation(200, 200);
		login.setTitle("Loginfenster");
		login.setVisible(true);
		login.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		
	}

	public Login() {
		
		loginPanel.setLayout(new GridLayout(0,2));
		add(loginPanel);
		loginPanel.add(lUsername);
		loginPanel.add(tfUsername);
		loginPanel.add(lPassword);

		loginPanel.add(tfPassword);
		loginPanel.add(bOK);
		loginPanel.add(bCancel);
		add(loginPanel,BorderLayout.CENTER);
		
		
	}
	
}

Gridlayout ist für Formulare weniger geeignet.

Ich würde dir hier das GridbagLayout empfehlen und die entsprechenden GridbagConstraints setzen.

Alternativ ist auch ein GUI-Builder zu empfehlen und sich die GUI zusammenzuklicken.

Besser als GridBagLayout ist [JAPI]GroupLayout[/JAPI]. Da passen sich die Komponenten besser (also ohne groß mit Constraints zu hantieren) auf ein ander an.
How to Use GroupLayout - Oracle

bye
TT

Edit von L-ectron-X:
Bitte zum Thema LayoutManager auch unser Wiki besuchen, da gibt es einige brauchbare Tutorials: LayoutManager der Standard-Java-API – Byte-Welt Wiki

Danke, kannte ich so noch nicht. Ging wohl ein wenig unter, da es erst mit Java 6 kam. Scheint aber gut zu sein.

Die besten ergebnisse erzielt man, wenn man Layouts schachtelt:[SPOILER]```public class GroupLayoutTest {
public static void main(String[] args) {
int n = 0;
showFrame(setSingleGroupLayout(new JFrame(“Group Layout Test”)), n++);
showFrame(setSingleGroupLayoutButtonsSeparated(new JFrame(“Group Layout Test”)), n++);
showFrame(setMixedLayout(new JFrame(“Group Layout Test”)), n++);
}

private static void showFrame(JFrame jFrame, int n) {
    jFrame.pack();
    jFrame.setLocation(n * 200, n * 150);
    jFrame.setVisible(true);
}

private static JFrame setSingleGroupLayout(JFrame jFrame) {
    JPanel jPanel = new JPanel();
    GroupLayout groupLayout = new GroupLayout(jPanel);
    groupLayout.setAutoCreateGaps(true);
    groupLayout.setAutoCreateContainerGaps(true);
    jPanel.setLayout(groupLayout);
    JLabel nameLable = new JLabel("Name:");
    JTextField nameInput = new JTextField(30);
    JLabel passLable = new JLabel("Passwort:");
    JTextField pasInput = new JPasswordField(20);
    JButton loginButton = new JButton("login");
    JButton cancelButton = new JButton("cancel");

    groupLayout.setHorizontalGroup(groupLayout
            .createSequentialGroup()
            .addGroup(
                    groupLayout.createParallelGroup().addComponent(nameLable).addComponent(passLable)
                            .addComponent(loginButton))
            .addGroup(
                    groupLayout.createParallelGroup().addComponent(nameInput).addComponent(pasInput)
                            .addComponent(cancelButton)));
    groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()
            .addGroup(groupLayout.createParallelGroup().addComponent(nameLable).addComponent(nameInput))
            .addGroup(groupLayout.createParallelGroup().addComponent(passLable).addComponent(pasInput))
            .addGroup(groupLayout.createParallelGroup().addComponent(loginButton).addComponent(cancelButton)));

    jFrame.add(jPanel);
    return jFrame;
}

private static JFrame setSingleGroupLayoutButtonsSeparated(JFrame jFrame) {
    JPanel jPanel = new JPanel();
    GroupLayout groupLayout = new GroupLayout(jPanel);
    groupLayout.setAutoCreateGaps(true);
    groupLayout.setAutoCreateContainerGaps(true);
    jPanel.setLayout(groupLayout);
    JLabel nameLable = new JLabel("Name:");
    JTextField nameInput = new JTextField(30);
    JLabel passLable = new JLabel("Passwort:");
    JTextField pasInput = new JPasswordField(20);
    JButton loginButton = new JButton("login");
    JButton cancelButton = new JButton("cancel");

    JPanel buttonGroup = new JPanel(new GridLayout(1, 0, 5, 5));
    buttonGroup.add(loginButton);
    buttonGroup.add(cancelButton);

    constructGroupLayout(groupLayout, nameLable, nameInput, passLable, pasInput, buttonGroup);

    jFrame.add(jPanel);
    return jFrame;
}

private static void constructGroupLayout(GroupLayout groupLayout, JLabel nameLable, JTextField nameInput,
        JLabel passLable, JTextField pasInput, JPanel buttonGroup) {
    groupLayout.setHorizontalGroup(groupLayout
            .createSequentialGroup()
            .addGroup(groupLayout.createParallelGroup().addComponent(nameLable).addComponent(passLable).addGap(0))
            .addGroup(
                    groupLayout.createParallelGroup().addComponent(nameInput).addComponent(pasInput)
                            .addComponent(buttonGroup)));
    groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()
            .addGroup(groupLayout.createParallelGroup().addComponent(nameLable).addComponent(nameInput))
            .addGroup(groupLayout.createParallelGroup().addComponent(passLable).addComponent(pasInput))
            .addGroup(groupLayout.createParallelGroup().addGap(0).addComponent(buttonGroup)));
}

private static JFrame setMixedLayout(JFrame jFrame) {
    JPanel jPanel = new JPanel();
    GroupLayout groupLayout = new GroupLayout(jPanel);
    groupLayout.setAutoCreateGaps(true);
    groupLayout.setAutoCreateContainerGaps(true);
    jPanel.setLayout(groupLayout);
    JLabel nameLable = new JLabel("Name:");
    JTextField nameInput = new JTextField(30);
    JLabel passLable = new JLabel("Passwort:");
    JTextField pasInput = new JPasswordField(20);
    JButton loginButton = new JButton("login");
    JButton cancelButton = new JButton("cancel");

    JPanel buttonGroup = new JPanel(new GridLayout(1, 0, 5, 5));
    buttonGroup.add(loginButton);
    buttonGroup.add(cancelButton);
    JPanel bottomPanel = new JPanel(new BorderLayout());
    bottomPanel.add(buttonGroup, BorderLayout.EAST);
    jFrame.add(bottomPanel, BorderLayout.SOUTH);

    constructGroupLayout(groupLayout, nameLable, nameInput, passLable, pasInput, bottomPanel);

    jFrame.add(jPanel);
    return jFrame;

}

}```[/SPOILER]
bye
TT