Usar JLabel

11 respostas
T

pessoal peguei uns códigos na net e deram alguns erros, o código está identico ao da apostila. Alguem poderia me ajudar? vlw…

import javax.swing.*;
import java.awt.*;
    
public class ExemploLabel extends JFrame
{
      JLabel rotulo1,rotulo2,rotulo3,rotulo4;
              
     public ExemploLabel()
     {
          super("Exemplo com Label");
         Container tela = getContentPane();
         setLayout(null);
         rotulo1 = new JLabel ("Nome");
         rotulo2 = new JLabel ("Idade");
         rotulo3 = new JLabel ("Telefone");
         rotulo4 = new JLabel ("Celular");
         rotulo1.setBounds(50,20,80,20);
         rotulo2.setBounds(50,60,80,20);
         rotulo3.setBounds(50,100,80,20);
         rotulo4.setBounds(50,140,80,20);
         rotulo1.setForeground(Color.red);
         rotulo2.setForeground(Color.blue);
         rotulo3.setForeground(new Color(190,152,142));
         rotulo4.setForeground(new Color(201,200,100));
         rotulo1.setFont(new Font("Arial",Font.BOLD,14));
         rotulo2.setFont(new Font("Comic Sans MS",Font.BOLD,16));
         rotulo3.setFont(new Font("Courier New",Font.BOLD,18));
         rotulo4.setFont(new Font("Times New Roman",Font.BOLD,20));
         tela.add(rotulo1);
         tela.add(rotulo2);
         tela.add(rotulo3);
         tela.add(rotulo4);
         setSize(400, 250);
         setVisible(true);
         setLocationRelativeTo(null);
    }


    public static void main(String args[])
   {

          ExemploLabel app = new ExemploLabel();
          app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

11 Respostas

Marlon_Meneses

nao ha nada d errado nesse codigo!

Marcio_Nogueira

Seu código está correto, não há erro.

cs.santos0

iai cara,

bom a primeira impressão, olhando o código por cima, não achei nenhum erro.

mas posta ai o erro q deu.

flw

T

ae galera esse é o erro.

java.lang.Error: Do not use ExemploLabel.setLayout() use ExemploLabel.getContentPane().setLayout() instead

at javax.swing.JFrame.createRootPaneException(JFrame.java:465)

at javax.swing.JFrame.setLayout(JFrame.java:531)

at ExemploLabel.(ExemploLabel.java:13)

at ExemploLabel.main(ExemploLabel.java:47)

Exception in thread mainProcess completed.
cs.santos0

Bom amigo, peguei o seu código e só tava faltando um parenteses.

tenta esse ai:

import javax.swing.*; 
import java.awt.*; 

public class ExemploLabel extends JFrame{ 

     JLabel rotulo1,rotulo2,rotulo3,rotulo4; 

     public ExemploLabel(){ 
          super("Exemplo com Label"); 
          Container tela = getContentPane(); 
          setLayout(null); 

          rotulo1 = new JLabel ("Nome"); 
          rotulo2 = new JLabel ("Idade"); 
          rotulo3 = new JLabel ("Telefone"); 
          rotulo4 = new JLabel ("Celular"); 

          rotulo1.setBounds(50,20,80,20); 
          rotulo2.setBounds(50,60,80,20); 
          rotulo3.setBounds(50,100,80,20); 
          rotulo4.setBounds(50,140,80,20); 

          rotulo1.setForeground(Color.red); 
          rotulo2.setForeground(Color.blue); 
          rotulo3.setForeground(new Color(190,152,142)); 
          rotulo4.setForeground(new Color(201,200,100)); 

          rotulo1.setFont(new Font("Arial",Font.BOLD,14)); 
          rotulo2.setFont(new Font("Comic Sans MS",Font.BOLD,16)); 

          //faltava um parenteses nessa linha!!
          rotulo3.setFont(new Font("Courier New",Font.BOLD,1)); 
          rotulo4.setFont(new Font("Times New Roman",Font.BOLD,20)); 

          tela.add(rotulo1); 
          tela.add(rotulo2); 
          tela.add(rotulo3); 
          tela.add(rotulo4); 

          setSize(400, 250); 
          setVisible(true); 
          setLocationRelativeTo(null); 
     }
 
     public static void main(String args[]){ 
          ExemploLabel app = new ExemploLabel(); 
          app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     } 
}
T

nao amigo essa linha é assim:

rotulo3.setFont(new Font(“Courier New”,Font.BOLD,18 )); // é que a escrita foi transformada em emoticon

Com o seu código aconteceu a mesma coisa. será talvez erro do JCreator?

quando mando compilar td ok, mas qdo mando rodar o projeto da esse erro.

T

alguem tem mais alguma idéia do que possa estar acontecendo?

cs.santos0

Cara, aki quando rodei, funcionou perfeitamente.

acho que é o seu JCreator
flw

T

sei lá intaum…o importante é q o código está certo…vou testar em outro IDE…vlw…

tnaires

Thiago Domingues:
ae galera esse é o erro.

java.lang.Error: Do not use ExemploLabel.setLayout() use ExemploLabel.getContentPane().setLayout() instead

at javax.swing.JFrame.createRootPaneException(JFrame.java:465)

at javax.swing.JFrame.setLayout(JFrame.java:531)

at ExemploLabel.(ExemploLabel.java:13)

at ExemploLabel.main(ExemploLabel.java:47)

Exception in thread mainProcess completed.</blockquote>

Cara o compilador tá reclamando porque você está usando o método setLayout().

Substitua o trecho:

super("Exemplo com Label"); Container tela = getContentPane(); setLayout(null);por

super("Exemplo com Label"); Container tela = getContentPane(); tela.setLayout(null);
Abraços

T

batata…era esse mesmo o erro…agora deu certinho…

Criado 5 de novembro de 2007
Ultima resposta 5 de nov. de 2007
Respostas 11
Participantes 5