Botões não aparecem na JFrame

3 respostas
O

O que tem de errado com meu código que os botões (na verdade só o btAdicionar que iniciei até agora) não aparecem na minha janela?

Está incompleto porque me deparei com esse problema.

Eu criei, iniciei, adicionei o .Bounds(x, y, tamanhoX, tamanhoY) e adicionei o botão no Painel. O que está faltando?

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Agenda extends JFrame implements ActionListener{
	
	private JTextField textDono;
	private JTextField textNome;
	private JTextField textTelefone;
	private JTextField textEmail;
	
	private JTextField cTextDono;
	private JTextField cTextNome;
	private JTextField cTextTelefone;
	private JTextField cTextEmail;
	
	private JLabel labelDono;
	private JLabel labelNome;
	private JLabel labelTelefone;
	private JLabel labelEmail;
	
	private JButton btAdicionar;
	private JButton btIncluir;
	private JButton btConsultar;
	private JButton btExcluir;
	private JButton btProximo;
	private JButton btAnterior;
	
	public void Janela() {
		//cria a janela principal
		JFrame frame = new JFrame();
		frame.setSize(400, 400);
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		
		getContentPane().setLayout(null); // não usar gerenciador de layout
		
		//inicia os campos de texto
		textDono = new JTextField(100);
		textNome = new JTextField(100);
		textTelefone = new JTextField(100);
		textEmail = new JTextField(100);
		
		cTextDono = new JTextField(100);
		cTextNome = new JTextField(100);
		cTextTelefone = new JTextField(100);
		cTextEmail = new JTextField(100);
		
		//inicia escrevendo na direita
		textDono.setHorizontalAlignment(textDono.RIGHT);
		textNome.setHorizontalAlignment(textNome.RIGHT);
		textTelefone.setHorizontalAlignment(textTelefone.RIGHT);
		textEmail.setHorizontalAlignment(textEmail.RIGHT);
		
		
		
		//define a cor de fundo do campo de texto
		textDono.setBackground(Color.white);
		textNome.setBackground(Color.white);
		textTelefone.setBackground(Color.white);
		textEmail.setBackground(Color.white);
		
		cTextDono.setBackground(Color.white);
		cTextNome.setBackground(Color.white);
		cTextTelefone.setBackground(Color.white);
		cTextEmail.setBackground(Color.white);
		
		//inicia as labels
		labelDono = new JLabel("Dono: ");
		labelNome = new JLabel("Nome: ");
		labelTelefone = new JLabel("Telefone: ");
		labelEmail = new JLabel("E-mail: ");
		
		//inicia os botões
		btAdicionar = new JButton("Adicionar Contatos");
		btIncluir = new JButton("Incluir");
		btConsultar = new JButton("Consultar Contatos");
		btExcluir = new JButton("Excluir contato");
		btProximo = new JButton();
		btAnterior = new JButton();
		
		
		//posicionamento dos campos
		btAdicionar.setBounds(10, 10, 100, 100);
		
		getContentPane().add(btAdicionar);
		
		

		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		
	}

}

Agradeço desde já!

3 Respostas

U

ophis o problema é que no método construtor vc está instanciando outro JFrame, causando assim certa confusão.
Pois sua classe já está estendida, desta forma ao invés de vc setar a classe principal vc está setando o objeto frame.

exclui a linha

e altera as linhas seguinte, para que os sets afetem a classe principal.

Diego_Adriano

Cara … de o setVisible depois de add o botao ao getcontentPane

getContentPane().add(btAdicionar); frame.setVisible(true);

O

Desculpe por demorar a responder.
Segui o conselho dos dois e deu certo.
Muito obrigado pela ajuda!

Abraço

Criado 25 de abril de 2013
Ultima resposta 27 de abr. de 2013
Respostas 3
Participantes 3