import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SwingFileDialog extends JFrame {
private JButton bDialog; //botão p/ acionar o diálogo
private JLabel bDialogo; //botão p/ resultados
private JFileChooser dialogo; //diálogo de arquivos
public SwingFileDialog() {
super("SwingFileDialog"); //ajusta titulo
Container cp = getContentPane(); //painel de conteudo
cp.setLayout(new GridLayout(2, 1)); //layout grade 4 x1
JLabel lResultado;
cp.add(lResultado = new JLabel("Sem seleção"));
lResultado.setBorder(
BorderFactory.createMatteBorder(2, 2, 3, 2, Color.GREEN));
bDialogo.addActionListener(new ActionListener() { //AQUI ESTA DANDO ERROR
public void actionPerformed(ActionEvent e) {
dialogo = new JFileChooser();
int res = dialogo.showOpenDialog(SwingFileDialog.this);
if(res==JFileChooser.APPROVE_OPTION) {
File arq = dialogo.getSelectedFile();
lResultado.setText(arq.getName());
} }
} );
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
public static void main(String[] args) {
new SwingFileDialog().setVisible(true);
}
}
Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method addActionListener(new ActionListener(){}) is undefined for the type JLabel
at SwingFileDialog.<init>(SwingFileDialog.java:29)
at SwingFileDialog.main(SwingFileDialog.java:43)