Chamando um metodo

9 respostas
ken420

pessoal criei esse metodo para abrir uma pequena tela quando pressionar o botao F a tela abre tudo ok mas nao aparece o meu jlabel pq?

public void botaoF()
    {
        JFrame telaF = new JFrame();
        Container tela = this.getContentPane();
        labelf1 = new JLabel("Cliente");
        labelf1.setBounds(30,10,50,20);
        labelf1.setFont(new Font("Serif", Font.BOLD, 14));
        labelf1.setForeground(new Color(255,255,0));
        tela.add(labelf1);
        telaF.setSize(250, 250);
        telaF.setVisible(true);
        telaF.setResizable(false);
        telaF.setLocationRelativeTo(null);
    }

9 Respostas

Lavieri

tenta adcionar isso

telaF.pack();

no final… e move o setVisible pra ser o ultimo comando… logodepois de pack();

ken420

veja como ficou

public void botaoF()
    {
        JFrame telaF = new JFrame();
        Container tela = this.getContentPane();
        labelf1 = new JLabel("Cliente");
        labelf1.setBounds(30,10,50,20);
        labelf1.setFont(new Font("Serif", Font.BOLD, 14));
        labelf1.setForeground(new Color(255,255,0));
        tela.add(labelf1);
        telaF.setPreferredSize(new Dimension(500, 500));
        telaF.setResizable(false);
        
        telaF.pack();
        telaF.setLocationRelativeTo(null);
        telaF.setVisible(true);
    }

com esse telaF.pack o painel n aparece so apareec a barra de titulo e nada em baixo ;/

ken420

mudei para isso e a tela volto abrir do jeito que eu queria porem os label nao aparece

public void botaoF()
    {
        JFrame telaF = new JFrame();
        Container tela = this.getContentPane();
        labelf1 = new JLabel("Cliente");
        labelf1.setBounds(30,10,50,20);
        labelf1.setFont(new Font("Serif", Font.BOLD, 14));
        labelf1.setForeground(new Color(255,255,0));
        tela.add(labelf1);
        telaF.setPreferredSize(new Dimension(500, 500));
        telaF.setResizable(false);
        
        telaF.pack();
        telaF.setLocationRelativeTo(null);
        telaF.setVisible(true);
    }
Lavieri
é que vc precisa adcionar 1 painel ao ContentPanel() ... algo assim
public void botaoF()  
{  
    JFrame telaF = new JFrame();
    labelf1 = new JLabel("Cliente");  
    labelf1.setBounds(30,10,50,20);  
    labelf1.setFont(new Font("Serif", Font.BOLD, 14));  
    labelf1.setForeground(new Color(255,255,0));  
    JPanel panelQueFaltava = new JPanel();
    panelQueFaltava.add(labelf1);  
    this.getContentPane().add(panelQueFaltava);  
    telaF.setPreferredSize(new Dimension(500, 500));  
    telaF.setResizable(false);  
      
    telaF.pack();  
    telaF.setLocationRelativeTo(null);  
    telaF.setVisible(true);  
}
ken420

fis exatamente o que vc me falo e chamei o metodo e nada nao aparee a jlabel :l eestranho…

Lavieri

a solução perfeita é… baixa o Jigloo

http://www.cloudgarden.com/jigloo/

se vc usa eclipse… Help->Software Updates->Find and Install

vai la em add site… e poem http://cloudgarden1.com/update-site

e instala ^^ be happy ^^

Dirceu_Roden

Na linha 4 você colocou:
Container tela = this.getContentPane();

altere para:
Container tela = telaF.getContentPane();

E ser for usar o setBounds() não se esqueça de setar o Layout para null:
telaF.setLayout(null);

ken420

no meu caso uso o netbeans ! :L

M

Faltou uma coisinha só:

public void botaoF()
    {
        JFrame telaF = new JFrame();
        Container tela = this.getContentPane(); // o gerenciador de leiaute padrão aqui é o BorderLayout!!!
        labelf1 = new JLabel("Cliente");
        labelf1.setBounds(30,10,50,20);
        labelf1.setFont(new Font("Serif", Font.BOLD, 14));
        labelf1.setForeground(new Color(255,255,0));
        tela.add(labelf1, BorderLayout.CENTER); // logo, faltou isso!!!
        telaF.setSize(250, 250);
        telaF.setVisible(true);
        telaF.setResizable(false);
        telaF.setLocationRelativeTo(null);
    }
Criado 2 de fevereiro de 2009
Ultima resposta 3 de fev. de 2009
Respostas 9
Participantes 4