Fórum Problema simples com jLabel #458637
17/10/2013
0
Estou tentando executar uma operacao simples lblNewLabel.setText("texto da label");
Mas o java eclipse esta me retornando o seguinte erro: lblNewLabel Cannot be resolved.
Nao estou conseguindo achar o problema alguem sabe me dizer?
import javax.swing.DefaultListModel;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
public class meuJFrame extends JFrame {
private DefaultListModel<String> lista = new DefaultListModel<String>();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
meuJFrame frame = new meuJFrame();
frame.setVisible(true);
frame.setLocationRelativeTo(null); //by eu - centraliza o jframe
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public meuJFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 549, 451);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
@Override
public void mouseClicked(MouseEvent e) {
// aki e o problema
lblNewLabel.setText("texto da label");
}
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(10, 259, 103, 14);
contentPane.add(lblNewLabel);
}
}
Everest10
Curtir tópico
+ 0Posts
17/10/2013
Derci Santos
Acho que o problema é que vc criou o o JLabel depois do método, para solucionar o problema tenta o código do jeito que está abaixo, pois assim o public void mouseClicked(MouseEvent e) vai conseguir utilizar o JLabel.
import javax.swing.DefaultListModel;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
public class meuJFrame extends JFrame {
private DefaultListModel<String> lista = new DefaultListModel<String>();
private JLabel lblNewLabel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
meuJFrame frame = new meuJFrame();
frame.setVisible(true);
frame.setLocationRelativeTo(null); //by eu - centraliza o jframe
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public meuJFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 549, 451);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
@Override
public void mouseClicked(MouseEvent e) {
// aki e o problema
lblNewLabel.setText("texto da label");
}
lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(10, 259, 103, 14);
contentPane.add(lblNewLabel);
}
}
Gostei + 0
17/10/2013
Everest10
package com.primeiroPrj.projeto;
//by eu
import javax.swing.DefaultListModel;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class meuJFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private DefaultListModel<String> lista = new DefaultListModel<String>();
private JPanel contentPane;
private JTextField txtSkill;
private JTextField txtLvl;
private JTextField txtName;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
meuJFrame frame = new meuJFrame();
frame.setVisible(true);
frame.setLocationRelativeTo(null); //by eu - centraliza o jframe
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public meuJFrame() {
addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
lista.addElement("9923-2-Berserker Spirit");
lista.addElement("9930-4-Mental Shield");
lista.addElement("9931-4-Resist Shock");
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 549, 451);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//-------------------------------------------
JButton btnAchaValor = new JButton("Procure");
btnAchaValor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnAchaValor.addMouseListener(new MouseAdapter() {
private String valItem = null;
@Override
public void mouseClicked(MouseEvent e) {
loop:
for (int i = 0; i < lista.getSize(); i++) {
valItem = lista.getElementAt(i);
String[] valsLista = valItem.split("-");
if (valsLista[0].equals(txtSkill.getText())) {
textField.setText("Skill encontrado: " + valsLista[2]);
break loop;
}
lblNewLabel.setText("aaa"); // aki esta o problema
}
}
});
//----------------------------------------
btnAchaValor.setBounds(123, 326, 335, 28);
contentPane.add(btnAchaValor);
JList<String> list = new JList<String>();
list.setModel(lista);
list.setBounds(123, 29, 335, 227);
contentPane.add(list);
txtSkill = new JTextField();
txtSkill.setBounds(119, 283, 64, 20);
contentPane.add(txtSkill);
txtSkill.setColumns(10);
txtLvl = new JTextField();
txtLvl.setBounds(193, 283, 86, 20);
contentPane.add(txtLvl);
txtLvl.setColumns(10);
txtName = new JTextField();
txtName.setBounds(290, 283, 168, 20);
contentPane.add(txtName);
txtName.setColumns(10);
textField = new JTextField();
textField.setBounds(149, 382, 244, 20);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(10, 259, 103, 14);
contentPane.add(lblNewLabel);
}
}
Gostei + 0
27/11/2013
Mario Giolo
new MouseAdapter() {
private String valItem = null;
@Override
public void mouseClicked(MouseEvent e) {
loop:
for (int i = 0; i < lista.getSize(); i++) {
valItem = lista.getElementAt(i);
String[] valsLista = valItem.split("-");
if (valsLista[0].equals(txtSkill.getText())) {
textField.setText("Skill encontrado: " + valsLista[2]);
break loop;
}
lblNewLabel.setText("aaa"); // Este atribto nao existe dentro desta classe
}
}
}
Corresponde a uma classe a parte, logo dentro desta classe, o atributo "lblNewLabel" não foi declarado, nem existe, e não tem qualquer relação com o lblNewLabel que você cria com:
JLabel lblNewLabel = new JLabel("New label");Logo, você precisa nesta classe Interna Anonima que você criou, de alguma forma obter a referencia ao "lblNewLabel" da classe externa, e quando o método alterar, deve ser o o valor referente a classe externa a ser alterado.
Entao eu defini o lblNewLabel como atributo da intancia, e criei um método get para ele, que é acionado internamente na classe interna anônima, ficando:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class meuJFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private DefaultListModel<String> lista = new DefaultListModel<String>();
private JPanel contentPane;
private JTextField txtSkill;
private JTextField txtLvl;
private JTextField txtName;
private JTextField textField;
private JLabel lblNewLabel;
public JLabel getLblNewLabel() {
return lblNewLabel;
}
public void setLblNewLabel(JLabel lblNewLabel) {
this.lblNewLabel = lblNewLabel;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
meuJFrame frame = new meuJFrame();
frame.setVisible(true);
frame.setLocationRelativeTo(null); //by eu - centraliza o jframe
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public meuJFrame() {
addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
lista.addElement("9923-2-Berserker Spirit");
lista.addElement("9930-4-Mental Shield");
lista.addElement("9931-4-Resist Shock");
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 549, 451);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//-------------------------------------------
JButton btnAchaValor = new JButton("Procure");
btnAchaValor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnAchaValor.addMouseListener(new MouseAdapter() {
private String valItem = null;
@Override
public void mouseClicked(MouseEvent e) {
loop:
for (int i = 0; i < lista.getSize(); i++) {
valItem = lista.getElementAt(i);
String[] valsLista = valItem.split("-");
if (valsLista[0].equals(txtSkill.getText())) {
textField.setText("Skill encontrado: " + valsLista[2]);
break loop;
}
getLblNewLabel().setText("aaa"); // aki esta o problema
}
}
});
//----------------------------------------
btnAchaValor.setBounds(123, 326, 335, 28);
contentPane.add(btnAchaValor);
JList<String> list = new JList<String>();
list.setModel(lista);
list.setBounds(123, 29, 335, 227);
contentPane.add(list);
txtSkill = new JTextField();
txtSkill.setBounds(119, 283, 64, 20);
contentPane.add(txtSkill);
txtSkill.setColumns(10);
txtLvl = new JTextField();
txtLvl.setBounds(193, 283, 86, 20);
contentPane.add(txtLvl);
txtLvl.setColumns(10);
txtName = new JTextField();
txtName.setBounds(290, 283, 168, 20);
contentPane.add(txtName);
txtName.setColumns(10);
textField = new JTextField();
textField.setBounds(149, 382, 244, 20);
contentPane.add(textField);
textField.setColumns(10);
lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(10, 259, 103, 14);
contentPane.add(lblNewLabel);
}
}
Uma boa ideia é você criar métodos que constroem algumas partes da tela, e entao você os chama dentro do construtor, para que o seu código fique mais claro e fácil de alterar.
Gostei + 0
27/11/2013
Mario Giolo
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)