Inserindo JPanel em JFrame

5 respostas
B

Estou criando minha classe Janela que extende de um JFrame. Quero inserir o panel no jframe e depois colocarei uma janela. Não tá rolando, adiciono o painel ao jframe mas fica o botão gigante na tela e não aparece mais nada. Alguém sabe me dizer oque falta?

public class Janela extends JFrame {

    JTextField jTxtField1 = new JTextField();

    public Janela() {
        setSize(300, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setTitle("JFrame, FUCK YEAH!");
        initComponents();
    }

    private void initComponents() {
        JPanel painel = new JPanel(new GridLayout(1, 3));
        JLabel jLabel1 = new JLabel("Pasta:");
        add(jLabel1);
        add(jTxtField1);
        JButton jButton1 = new JButton("Atualizar");
        add(jButton1);
        this.add(painel, BorderLayout.NORTH);
    }

    public static void main(String[] args) {
        new Janela().setVisible(true);
    }
}

5 Respostas

H

Você precisa trabalhar com um dos layout managers:

http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

[]'s

A

tente fazer getContentPane().add(jpane), ao invés de somente chamar o método add.

B

GridLayout é um tipo de layoutManager, não?

E mesmo inserindo com getContentPane dá a mesma coisa.

:frowning:

J

Tente utilizar seu codigo assim:

public class Janela extends JFrame {   
  
    JTextField jTxtField1 = new JTextField(10);   
    JLabel jLabel1 = new JLabel("Pasta:");   
    JButton jButton1 = new JButton("Atualizar");   
    
    public Janela() {   
        JPanel painel = new JPanel();  
        painel.setLayout(new GridLayout(7,2));
        
        JPanel painel2 = new JPanel();
        painel2.setLayout(new FlowLayout(FlowLayout.LEFT));
        
        painel2.add(jLabel1);
        painel2.add(jTxtField1); 
        
        painel.add(painel2);
        painel.add(jButton1);
        
        this.setContentPane(painel); 
        this.setSize(310,220);   
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);   
        this.setLocationRelativeTo(null);   
        this.setTitle("JFrame, FUCK YEAH!"); 
    }    
  
    public static void main(String[] args) {   
        new Janela().setVisible(true);   
    }   
}

Para inserir no Frame é necessário estar dentro do construtor,
e também para inserir layout e utilizar “add” é necessário indicar
qual objeto pretende-se utilizar.

espero que tenha ajudado.

I

juiner, não é necessário estar dentro do construtor, além do mais, o initComponents já tá lá dentro.
[BlacK], seu erro foi uma mera distração:

private void initComponents() {   
        JPanel painel = new JPanel(new GridLayout(1, 3));   
        JLabel jLabel1 = new JLabel("Pasta:");   
        add(jLabel1);   
        add(jTxtField1);   
        JButton jButton1 = new JButton("Atualizar");   
        add(jButton1);   
        this.add(painel, BorderLayout.NORTH);   
    }

Tá vendo o método add?Em this.add, vc adiciona o painel, mas os componentes NÃO foram corretamente passados a esse JPanel painel(estão sendo colados um sobre o outro).Ou seja, é só pôr painel.add(jLabel1); , painel.add(jTxtField1); e assim para todos os componentes que eles serão inseridos naturalmente no GridLayout.

Criado 26 de março de 2009
Ultima resposta 28 de mar. de 2009
Respostas 5
Participantes 5