GridBagLayout. Campos desalinhados

0 respostas
Allan_BSO

Pessoal,
Estou com um grande problema.. estou comecando a ver a parte gráfica awt/swing.
ao criar um formulario os meus labels nao ficam ajustados com as minhas caixas de texto... usando o gerenciador GridBagLayout.
Achei estranho, pois o pessoal costuma dizer que o gridbaglayout costuma ser flexivel, apesar de mais complexo...

da uma olhada...

import java.awt.*;
import java.awt.event.*;

//Classe
public class TrabalhoWordQuestao15 extends Frame{
	
	//LABEL
	/* ----------------------------------- */
	
	private Label lblNome = new Label("Nome:");
	private Label lblEndereco = new Label("Endereco:");
	private Label lblEndNumero = new Label("Número:");
	private Label lblBairro = new Label("Bairro:");
	private Label lblIdade = new Label("Idade:");
	private Label lblSexo = new Label("Sexo:");
	private Label lblEmail = new Label("Email :");
	private Label lblFone = new Label("Fone:");
	private Label lblCelular = new Label("Celular:");
	private Label lblInformeSistema = new Label("Informe os Sistemas que utiliza:");
	private Label lblNaturalidade = new Label("Naturalidade:");
	private Label lblUf = new Label("UF:");
	private Label lblCep = new Label("CEP:");
	

	//TEXT FIELD
	/* ----------------------------------- */
	
	private TextField txtNome = new TextField(30);
	private TextField txtEndereco = new TextField(30);
	private TextField txtEndNumero = new TextField(15);
	private TextField txtBairro = new TextField(30);
	private TextField txtIdade = new TextField(20);
	private TextField txtEmail = new TextField(60);
	private TextField txtFone = new TextField(15);
	private TextField txtDDDFone = new TextField(5);
	private TextField txtCelular = new TextField(15);
	private TextField txtDDDCelular = new TextField(5);
	private TextField txtNaturalidade = new TextField(15);
	private TextField txtCep = new TextField(15);
	
	
	//CHOICE
	/* ----------------------------------- */
	
	private Choice txtUf = new Choice();
	
	
	//GRUPO CHECK BOX-RADIOs
	/* ----------------------------------- */
	
	private CheckboxGroup cbgSexo = new CheckboxGroup();
	private Checkbox cbMasculino;
	private Checkbox cbFeminino;
	
	
	//CHECKBOX
	/* ----------------------------------- */
	
	private Checkbox cbWindows = new Checkbox("Windows");
	private Checkbox cbLinux = new Checkbox("Linux");
	private Checkbox cbUnix = new Checkbox("Unix");
	
	
	
	// Contrutor da Classe
	public TrabalhoWordQuestao15(){
		super("Layout Exercicio15 - Prof Vladimir");
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		
		this.setSize(800, 600);
		this.setLayout(new GridBagLayout());
		GridBagConstraints cell = new GridBagConstraints();
		
		
		//ADICIONANDO COMPONENTES AO LAYOUT;
		
		
		//Labels
		/* ----------------------------------- */	
		
		cell.gridx = 0;
		cell.gridy = 0;
		this.add(lblNome,cell);
		
		cell.gridx = 0;
		cell.gridy = 1;
		this.add(lblEndereco,cell);
	
		cell.gridx = 4;
		cell.gridy = 0;
		this.add(lblEndNumero,cell);
				
		cell.gridx = 0;
		cell.gridy = 2;
		this.add(lblBairro,cell);
		
		cell.gridx = 0;
		cell.gridy = 3;
		this.add(lblIdade,cell);
		
		cell.gridx = 0;
		cell.gridy = 4;
		this.add(lblSexo,cell);
		
		cell.gridx = 0;
		cell.gridy = 5;
		this.add(lblEmail,cell);
		
		cell.gridx = 0;
		cell.gridy = 6;
		this.add(lblFone,cell);
		
		cell.gridx = 3;
		cell.gridy = 6;
		this.add(lblCelular,cell);
		
		cell.gridx = 0;
		cell.gridy = 7;
		cell.gridwidth = 3;
		this.add(lblInformeSistema,cell);
		
		cell.gridx = 3;
		cell.gridy = 3;
		this.add(lblNaturalidade,cell);
		
		cell.gridx = 4;
		cell.gridy = 2;
		this.add(lblUf,cell);
		
		cell.gridx = 4;
		cell.gridy = 1;
		this.add(lblCep,cell);
		
		
		//TextFields
		/* ----------------------------------- */
		
		cell.gridx = 1;
		cell.gridy = 0;
		cell.gridwidth = 3;
		this.add(txtNome,cell);
		
		cell.gridx = 1;
		cell.gridy = 1;
		cell.gridwidth = 3;
		this.add(txtEndereco,cell);
		
		cell.gridx = 5;
		cell.gridy = 0;
		this.add(txtEndNumero,cell);
		
		cell.gridx = 5;
		cell.gridy = 1;
		this.add(txtCep,cell);
			
		cell.gridx = 1;
		cell.gridy = 2;
		cell.gridwidth = 3;
		this.add(txtBairro,cell);
		
		cell.gridx = 1;
		cell.gridy = 3;
		cell.gridwidth = 2;
		this.add(txtIdade,cell);
		
		cell.gridx = 4;
		cell.gridy = 3;
		cell.gridwidth = 2;
		this.add(txtNaturalidade,cell);
		
		cell.gridx = 1;
		cell.gridy = 5;
		cell.gridwidth = 5;
		this.add(txtEmail,cell);
		
		cell.gridx = 1;
		cell.gridy = 6;
		this.add(txtDDDFone,cell);
		
		cell.gridx = 2;
		cell.gridy = 6;
		this.add(txtFone,cell);
		
		cell.gridx = 4;
		cell.gridy = 6;
		this.add(txtDDDCelular,cell);
		
		cell.gridx = 5;
		cell.gridy = 6;
		this.add(txtCelular,cell);
		
		
		pack();
		
	}
	
	// Classe Main
	public static void main(String args[]){
		TrabalhoWordQuestao15 gui = new TrabalhoWordQuestao15();
		gui.setVisible(true);
	}
	
	
	
	// ...
}

por favor, alguém pode me dizer como eu posso criar esse layout com esse tipo de gerenciador...
:wink:

obrigado!

grato.

Criado 10 de setembro de 2008
Respostas 0
Participantes 1