Fenster winzig klein durch GridBagLayout

Lit-Web

Aktives Mitglied
Guten Tag @ all,

ich bin grad dabei mir ein kleines Progrämmchen mit einem Login zu programmieren.

Meinem Frame habe ich ein Borderlayout verpasst und ein Panel da im CENTER plaziert.
Dem Panel habe ich dann ein GridBagLayout gegeben.
Das gesamte Login Panel mit Feldern etc. habe ich in eine extra Klasse ausgelagert und übergebe diese dann an richtiger Stelle in der Frame Klasse.
Die Frame Klasse rufe ich dann im Main auf.

Hier mal der die Main.
Java:
public class Main {
	
	public static void main ( String [] args ) {
//		DatabaseConnect dbCon = new DatabaseConnect( "ConfigFiles/mysqlProperties.txt" );
		
		new ProgramFrame();
	}
}

Hier die Frame Klasse.
Java:
import javax.swing.*;
import java.awt.*;

public class ProgramFrame {
	
	protected JFrame masterFrame;
	protected int logged = 0;
	
	public ProgramFrame () {
		this.createMasterFrame();
	}
	
	protected JFrame createMasterFrame () {
		masterFrame = new JFrame( "Firma K & S" );
		masterFrame.setSize( 500, 500 );
		masterFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		
		if ( logged == 0 ) {
			masterFrame.setLayout( new BorderLayout() );
			masterFrame.add( new LoginGuiElement(), BorderLayout.CENTER );
		} else {
			masterFrame.setLayout( new BorderLayout() );
		}

		masterFrame.pack();
		masterFrame.setLocationRelativeTo( null );
		masterFrame.setVisible( true );
		return masterFrame;
	}	
}

Und die Login Panel Klasse
Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LoginGuiElement extends JPanel {
	
	protected JLabel lblLogName;
	protected JLabel lblPwd;
	protected GridBagLayout pnlLoginLayout;
	protected JTextField loginNameField;
	protected JTextField loginPwdField;
//	protected JButton loginButton;
	
	public LoginGuiElement () {
		this.setSize( 500, 500 );
		this.setLayout( this.createLogPanelLayout() );
		this.setBackground( Color.white );
		this.setVisible( true );
		this.add( this.returnLblLogName() );
		this.add( this.returnLblPwd() );
		this.add( this.returnNameField() );
	}
	
	protected JLabel returnLblLogName () {
		lblLogName = new JLabel();
		Font f = new Font( "Arial", Font.BOLD, 12 );
		lblLogName.setText( "Loginname" );
		lblLogName.setSize( 120, 20 );
		lblLogName.setFont( f );
		lblLogName.setForeground( Color.black );
		lblLogName.setLocation( 10, 40 );
		lblLogName.setVisible( true );
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx = 120;
		gbc.weighty = 20;
		pnlLoginLayout.setConstraints( lblLogName, gbc );
		return lblLogName;
	}
	
	protected JLabel returnLblPwd () {
		lblPwd = new JLabel();
		Font f = new Font( "Arial", Font.BOLD, 12 );
		lblPwd.setText( "Passwort" );
		lblPwd.setSize( 120, 20 );
		lblPwd.setFont( f );
		lblPwd.setForeground( Color.black );
		lblPwd.setLocation( 10, 40 );
		lblPwd.setVisible( true );
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx = 120;
		gbc.weighty = 20;
		pnlLoginLayout.setConstraints( lblPwd, gbc );
		return lblPwd;
	}
	
	protected JTextField returnNameField () {
		loginNameField = new JTextField();
		loginNameField.setSize( 120, 20 );
		loginNameField.setVisible( true );
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx = 120;
		gbc.weighty = 20;
		pnlLoginLayout.setConstraints( loginNameField, gbc );
		return loginNameField;
	}
	
	protected GridBagLayout createLogPanelLayout () {
		pnlLoginLayout = new GridBagLayout();
		return pnlLoginLayout;
	}
}

So und das kleine anghängte Bildchen zeigt was dabei raus kommt. Obwohl ich dem Frame Höhe und Breite gegeben habe, wird das ganze Frame auf Minigröße geschrumpft.
Warum passiert das, ich habe schon viel gesucht, aber nicht wirklich meinen Fehler gefunden, kann auch sein das ich einfach nur blind war. Vieleicht sieht es jemand von euch was das da gegen die Wand fahren lässt.

Vielen Dank für die Hilfe.

Mfg Lit-Web
 

Anhänge

  • java_frame_login_panel.JPG
    java_frame_login_panel.JPG
    3,7 KB · Aufrufe: 23
Wenn du layoutmanager verwendest, dann musst du setPreferredSize nutzen. setSize hat hier keinen einfluss (das wird vom layoutmanager einfach überschrieben).
 
Cool danke,

diese Info habe ich nirgends gefunden, oder eben wirklich überlesen. Nun klappt das wie es soll.

Hier die veränderte Login Panel Klasse für die Nachwelt.

Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LoginGuiElement extends JPanel {
	
	protected JLabel lblLogName;
	protected JLabel lblPwd;
	protected GridBagLayout pnlLoginLayout;
	protected JTextField loginNameField;
	protected JTextField loginPwdField;
	protected JButton loginButton;
	
	public LoginGuiElement () {
		this.setSize( 500, 500 );
		this.setLayout( this.createLogPanelLayout() );
		this.setPreferredSize( new Dimension( 500, 150 ) );
		this.setBackground( Color.white );
		this.setVisible( true );
		this.add( this.returnLblLogName() );
		this.add( this.returnLblPwd() );
		this.add( this.returnNameField() );
		this.add( this.returnPwdField() );
		this.add( this.returnBtnLogin() );
	}
	
	protected JLabel returnLblLogName () {
		lblLogName = new JLabel();
		Font f = new Font( "Arial", Font.BOLD, 12 );
		lblLogName.setText( "Loginname" );
		lblLogName.setSize( 120, 20 );
		lblLogName.setFont( f );
		lblLogName.setForeground( Color.black );
		lblLogName.setLocation( 10, 40 );
		lblLogName.setVisible( true );
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx = 100;
		gbc.weighty = 20;
		pnlLoginLayout.setConstraints( lblLogName, gbc );
		return lblLogName;
	}
	
	protected JLabel returnLblPwd () {
		lblPwd = new JLabel();
		Font f = new Font( "Arial", Font.BOLD, 12 );
		lblPwd.setText( "Passwort" );
		lblPwd.setSize( 120, 20 );
		lblPwd.setFont( f );
		lblPwd.setForeground( Color.black );
		lblPwd.setLocation( 10, 40 );
		lblPwd.setVisible( true );
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx = 100;
		gbc.weighty = 20;
		pnlLoginLayout.setConstraints( lblPwd, gbc );
		return lblPwd;
	}
	
	protected JTextField returnNameField () {
		loginNameField = new JTextField();
		loginNameField.setPreferredSize( new Dimension( 120, 20 ) );
		loginNameField.setVisible( true );
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx = 120;
		gbc.weighty = 20;
		pnlLoginLayout.setConstraints( loginNameField, gbc );
		return loginNameField;
	}
	
	protected JTextField returnPwdField () {
		loginPwdField = new JTextField();
		loginPwdField.setPreferredSize( new Dimension( 120, 20 ) );
		loginPwdField.setVisible( true );
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx = 120;
		gbc.weighty = 20;
		pnlLoginLayout.setConstraints( loginPwdField, gbc );
		return loginPwdField;
	}
	
	protected JButton returnBtnLogin () {
		loginButton = new JButton();
		loginButton.setPreferredSize( new Dimension( 120, 20 ) );
		loginButton.setText( "Login" );
		loginButton.setVisible( true );
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 1;
		gbc.gridy = 2;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx = 120;
		gbc.weighty = 20;
		pnlLoginLayout.setConstraints( loginButton, gbc );
		return loginButton;
	}
	
	protected GridBagLayout createLogPanelLayout () {
		pnlLoginLayout = new GridBagLayout();
		return pnlLoginLayout;
	}
}

Und noch ein Bild als Anhang wie es nach der Änderung aussieht.

Mfg Lit-Web
 

Anhänge

  • loginPanelNeu.JPG
    loginPanelNeu.JPG
    10,3 KB · Aufrufe: 25

Neue Themen


Zurück
Oben