Sou iniciante e estou estudando o livro Deitel.
Eu uso o IDE NetBeans 6.1.
No Cap.: 11 (GUI)
Copiei isso:
[code]package demonstrandoaclassejlabel;
import java.awt.FlowLayout;// especifica como os componentes são organizados.
import javax.swing.JFrame;//fornece recursos básicos de janela.
import javax.swing.JLabel;//exibe textos e imagens.
import javax.swing.SwingConstants;//Constantes comuns utilizadas com swing.
import javax.swing.Icon;//interface utilizada para manipular imagens.
import javax.swing.ImageIcon;//Carrega imagens.
public class LabelFrame extends JFrame
{
private JLabel Label1;// JLabel apenas com texto.
private JLabel Label2;// JLabel Construído com texto e ícone.
private JLabel Label3;// JLabel com texto e ícones adicionados.
// Costrutor LabelFrame adiciona JLabels a JFrame
public LabelFrame()
{
super ("Teste JLabel");
setLayout(new FlowLayout()); //configura Layout de Frame.
// Construtor JLabel com um argumento de String.
Label1 = new JLabel ("Label whit Test");
Label1.setToolTipText("esse é o Label");
add(Label1);// adiciona o Label ao Frame.
// construtor Jlabel com String, ícon e argumentos de alinhamento.
Icon bug = new ImageIcon(getClass().getResource("violentos.gif"));
Label2 = new JLabel("Label whit Test and icon",bug,
SwingConstants.LEFT);
Label2.setToolTipText("Esse é o Label 2");
add(Label2);//adiciona o Labe2 ao Frame.
Label3 = new JLabel();//Construtor JLabel sem argumentos.
Label3.setText("Label whit icon and text at botton");
Label3.setIcon(bug);// adiciona o icon ao JLabel.
Label3.setHorizontalTextPosition(SwingConstants.CENTER);
Label3.setVerticalTextPosition(SwingConstants.BOTTOM);
Label3.setToolTipText("Esse é o Label 3");
add(Label3); //adiciona o Labe3 ao Frame.
}
}
package demonstrandoaclassejlabel;
/**
*
* @author Usuário
*/
import javax.swing.JFrame;
public class LabelText
{
public static void main (String args[])
{
LabelFrame labelFrame = new LabelFrame(); // Cria LabelFrame.
labelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
labelFrame.setSize(275,180);//configura o tamanho do Frame.
labelFrame.setVisible(true); // exibe o Frame.
}
}
Mas não funciona!
Aparece isso:
init:
deps-jar:
compile:
run:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.
at demonstrandoaclassejlabel.LabelFrame.
at demonstrandoaclassejlabel.LabelText.main(LabelText.java:23)
Java Result: 1
CONSTRUÍDO COM SUCESSO (tempo total: 1 segundo)
O QUE ESTÁ ERRADO?
