我试图在netbeans上使用不同的类,一个是我的GUI(也,我开始编程,刚刚学会了什么是一个GUI),在这个,我有一些jTextField的和JFormattedTextField的,另一个,我想使用作为后端,对于初学者,我想删除我已经在我的DB上的信息。
我得到的错误是:错误:找不到符号如果(jFormattedTextFieldCPF.getText () .equals(";")){符号:变量jFormattedTextFieldCPFlocation: class Modos
这是我的代码的第一部分:
private void jButtonExcluirActionPerformed(java.awt.event.ActionEvent evt) {
MODO = 1;// it sends which action the button have to do
funcao.funcoes(MODO);
// TODO add your handling code here:
}
在类Modos中,我只有一个名为funces的函数,其中将有更多的动作要做我的DB,如编辑,添加,搜索…
public void funcoes(int MODO) {
if (MODO == 1){
PreparedStatement stm;
try {
/*jFormattedTextFieldCPF.setEnabled(true);
jTextFieldNOME.setEnabled(false);
jTextFieldIDADE.setEnabled(false);
jFormattedTextFieldDATA.setEnabled(false);
jTextFieldAPELIDO.setEnabled(false);
*/
stm = conecta.conn.prepareStatement("delete from cad_pessoa where cad_cpf=?");
if(jFormattedTextFieldCPF.getText().equals("")){
JOptionPane.showMessageDialog(null, "Por favor completar o campo CPF");
conecta.conn.rollback();
}
else {
stm.setString(1, jFormattedTextFieldCPF.getText()); //pega o nome que será deletado
stm.execute(); //executa o SQL
conecta.conn.commit();
JOptionPane.showMessageDialog(rootPane, "Excluído!");
/*jFormattedTextFieldCPF.setText("");
jTextFieldNOME.setText(""); //deixa o campo vazio
jTextFieldIDADE.setText("");
jFormattedTextFieldDATA.setText("");//deixa o campo vazio
jTextFieldAPELIDO.setText(""); //deixa o campo vazio
jFormattedTextFieldCPF.setEnabled(false); //deixa o campo indisponivel
jTextFieldNOME.setEnabled(false);
jTextFieldIDADE.setEnabled(false);
jFormattedTextFieldDATA.setEnabled(false);
jTextFieldAPELIDO.setEnabled(false);
jButtonINSERIR.setEnabled(true);
jButtonALTERAR.setEnabled(false);*/
// as a comentary cuz everything is a jFrame and gives an error
}
} catch (SQLException ex) {
Logger.getLogger(Pessoa.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Erro:01n" + ex.getMessage());
}
}
很抱歉长问题,为了简化,我想在另一个类中获得jFormattedTextField的信息。
你可以声明一个jTextField类型的静态变量,在你的方法中初始化这个静态变量,修改后使用相同的jFormattedTextField对象,声明一个静态get方法从另一个类获取这个变量并调用它。
class WhereYourJTextExists{
private jTextField jText ;
private static jTextField jToPass;
public static jTextField getJToPass(){
return jToPass;
}
public void yourTaskHere(){
// .... do some tasks with jText
// initialize your variable with the jTextField
jToPass= jText;
}
}
// ----------------------------------
class OtherClass{
public void needJtextHere(){
jTextField recievedJtext = WhereYourJTextExists.getJToPass();
//...do your job with the recived jTextField
}
}
解耦代码。传递funcoes
需要的所有信息,以便完成它的工作,并让它根据需要抛出一个或多个异常,例如…
public void funcoes(int MODO, String cpf) throws SQLException {
if (MODO == 1) {
if (cpf.equals("")) {
// This could be a custom exception, which would make it easier
// to detect and display a custom error message
throw new SQLException("Por favor completar o campo CPF");
} else {
try (PreparedStatement stm = conecta.conn.prepareStatement("delete from cad_pessoa where cad_cpf=?")) {
stm.setString(1, jFormattedTextFieldCPF.getText()); //pega o nome que será deletado
stm.execute(); //executa o SQL
conecta.conn.commit();
}
}
}
}