package visual;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class janelaLogin extends JFrame{
public janelaLogin(){
super("Login");
}
public void cria_janela_login(){
// GERENCIADOR DOS CAMPOS E LABEL
FlowLayout gerenciadorCamposeLabel = new FlowLayout();
JPanel campoeLabel = new JPanel();
campoeLabel.setLayout(gerenciadorCamposeLabel);
// ADICIONA COMPONETES
campoeLabel.add(new JLabel("Login:"));
campoeLabel.add(new JTextField(10));
campoeLabel.add(new JLabel("Senha:"));
campoeLabel.add(new JPasswordField(10));
getContentPane().add(campoeLabel);
// -------------------
// GERENCIADOR DOS BOTÕES
FlowLayout gerenciadorBotoes = new FlowLayout();
JPanel botoes = new JPanel();
botoes.setLayout(gerenciadorBotoes);
// ADICIONA COMPONETES
botoes.add(new JButton("Entrar"));
botoes.add(new JButton("Novo Cadastro"));
getContentPane().add(botoes);
// --------------------
setSize(220,150);
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String args[]){
janelaLogin s = new janelaLogin();
s.cria_janela_login();
}
}
Meus componentes do Layout não estão aparecendo
7 Respostas
this.setLayout(new FlowLayout());
add um Layout para seu JFrame
lembre e nunca esqueca de add o seu JFramesetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Não ta aparecendo pq vc não adicionou no GUI
troque seu codigo:
getContentPane().add(campoeLabel);
para
getContentPane().add(botoes, BorderLayout.SOUTH);
getContentPane().add(campoeLabel, BorderLayout.CENTER);
Aconselho a vc ler e estudar pelo menos um livro ou tutorial de Swing…
Legal, funcionou perfeitamente FernandoFranzini e mauriciot.silva
FernandoFranzini Você me indicaria algum livro?
Abraço!
Pessoal,
Não entendi porque tive que adicionar,
Sendo que se eu orientar “gerenciadorCamposeLabel” em “LEFT” ele repeita o “LEFT” e não o “CENTER” do BorderLayout
// GERENCIADOR DOS CAMPOS E LABEL
FlowLayout gerenciadorCamposeLabel = new FlowLayout(FlowLayout.LEFT); // new FlowLayout(FlowLayout.LEFT); < ----------
JPanel campoeLabel = new JPanel();
campoeLabel.setLayout(gerenciadorCamposeLabel);
Será que eu realmente preciso das linhas 2 e 3? do “//GERENCIADOR DOS CAMPOS E LABEL”
Boa noite a todos.
Se você definiu o layout do seu JPanel como FlowLayout, então ele obedecerá este layout, contudo você não definiu qual o layout da sua janela principal.
Você deveria tão somente trocar a sua linha de comando:
// Troque
getContentPane().add(compeLabel);
// Para
setContentPane(compoeLabel);
Tão somente isto, e ai você não precisa definir o layout do Janela Principal, agora se você quer que este JPanel aparece em algum canto da Janela principal, neste caso você deve:
// Primeiro defina o layout da Janela Principal.
setLayout(new BorderLayout());
// Agora você pode utilizar os recursos do BorderLayout
getContentPane().add(getContentPane().add(botoes, BorderLayout.SOUTH);
getContentPane().add(campoeLabel, BorderLayout.CENTER);
Você pode utilizar qualquer outro layout na Janela Principal, desde que você o defina no método setLayout() primeiro.
Outra coisa, você poderia instanciar o seu JLabel desta maneira:
JPanel compoeLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
Veja quanta variável, memória e código você poupou.
Valeu pelas dicas galera!
Grande abraço!