Programa n compila. ajud aplease =[

3 respostas
Elizeu_Santos

galera to lendo a apostila FJ-11 da caelum. sou nub, estou migrando de delphi pra java e é brabo hein! to lendo sobre o swing e o seguinte código n compila por nda =[

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Formulario {
	private JLabel label;
	private JTextField textField;
	private JButton button;

	private JPanel panel;
	private JFrame frame;

	private void montaFormulario() {
		label = new JLabel("Seu nome: ");
		textField = new JTextField(20);
		button = new JButton("Exibir");

		button.addActionListener(this);

		panel = new JPanel();
		panel.add(label);
		panel.add(textField);
		panel.add(button);

		frame = new JFrame("Meu Primeiro Formulário");
		frame.add(panel);

		frame.pack();
		frame.setVisible(true);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent e) {
		String texto = textField.getText();
		JOptionPane.showMessageDialog(null, texto);
	}

	public static void main(String[] args) {
		new Formulario().montaFormulario();
	}

}

pelas barbas do tubarão alguem poderia me ajudar? desde ja agradeço ^^.

3 Respostas

Z

Olá amigo, implementa na sua classe a interface ActionListener, e vê se funfa.

public class Formulario implements ActionListener{}

abss

Pedrosa

Troque isso:

button.addActionListener(this); //aqui não compila

Por isso

button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String texto = textField.getText();
				JOptionPane.showMessageDialog(null, texto);
			}
});
Elizeu_Santos

obrigado ^^. a primeira forma é + pratica… + a segunda da pra entender melhor a situação. obrigado

Criado 8 de maio de 2009
Ultima resposta 8 de mai. de 2009
Respostas 3
Participantes 3