>
Olá,
Primeiramente quero parabenizar a RL System pela qualidade e didática das video aulas durante todo o curso em questão.
Na aula 10, porque não consigo utilizar a palavra "this" para referenciar a variável: private JTextField txtNome da minha classe Cadastro ?
Desde já muito obrigado!
pode postar seu código?
Boa noite Marcel, desculpe-me pale demora!
1º Classe Cliente:
package model;
import java.io.File;
import java.io.FileWriter;
public class Cliente {
private String nome;
private String email;
private int idade;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getIdade() {
return idade;
}
public void setIdade(int idade) {
this.idade = idade;
}
public String salvar() {
File file = new File(\"C:\\\\curso-basico\\\\cadastrados\\\\salvo.txt\");
if(!file.exists()) {
try {
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(\"Nome: \" + this.nome);
fileWriter.write(System.lineSeparator());
fileWriter.write(\"Email: \" + this.email);
fileWriter.write(System.lineSeparator());
fileWriter.write(\"Idade: \" + this.idade + \" anos\");
fileWriter.flush();
fileWriter.close();
}catch (Exception e) {
System.out.println(\"Ocorreu um erro no sistema: \" + e.getMessage());
}
}
return \"Cliente cadastrado com sucesso!\";
}
}
2º JFrame (\"Telinnha\")
package view;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import model.Cliente;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Cadastro {
//minha dúvida é porque não consigo usar o \"this\" nas variáveis abaixo, quando as chamo dentro do metodo actionPerformed.
private JFrame frame;
private JTextField txtNome;
private JTextField txtEmail;
private JLabel labelIdade;
private JTextField txtIdade;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cadastro window = new Cadastro();
window.frame.setVisible(true);
window.frame.setLocation(500, 200);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Cadastro() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel labelNome = new JLabel(\"Nome:\");
labelNome.setBounds(10, 10, 40, 15);
frame.getContentPane().add(labelNome);
txtNome = new JTextField();
txtNome.setFont(new Font(\"Verdana\", Font.PLAIN, 11));
txtNome.setBounds(50, 7, 365, 20);
frame.getContentPane().add(txtNome);
txtNome.setColumns(10);
JLabel labelEmail = new JLabel(\"Email:\");
labelEmail.setBounds(10, 50, 40, 15);
frame.getContentPane().add(labelEmail);
txtEmail = new JTextField();
txtEmail.setBounds(50, 47, 365, 20);
frame.getContentPane().add(txtEmail);
txtEmail.setColumns(10);
labelIdade = new JLabel(\"Idade:\");
labelIdade.setBounds(10, 90, 40, 15);
frame.getContentPane().add(labelIdade);
txtIdade = new JTextField();
txtIdade.setBounds(50, 87, 65, 20);
frame.getContentPane().add(txtIdade);
txtIdade.setColumns(10);
JButton btnCadastrar = new JButton(\"Cadastrar\");
btnCadastrar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Cliente cliente = new Cliente();
cliente.setNome(txtNome.getText());
cliente.setEmail(txtEmail.getText());
cliente.setIdade(Integer.parseInt(txtIdade.getText()));
JOptionPane.showMessageDialog(null, cliente.salvar());
txtNome.setText(\"\");
txtEmail.setText(\"\");
txtIdade.setText(\"\");
}
});
btnCadastrar.setBackground(SystemColor.activeCaption);
btnCadastrar.setBounds(315, 210, 100, 40);
frame.getContentPane().add(btnCadastrar);
}
}
Desde já muito obrigado!