我正在嘗試使用 netbeans 上的不同類,一個是我的 GUI(而且,我開始編程并剛剛了解了一個 GUI),在這個類中,我有一些 jTextField 和 JFormattedTextField,另一個,我想用作后端,對于初學者,我想洗掉我的資料庫中已有的資訊。
我得到的錯誤是這樣的:
錯誤:找不到符號 if(jFormattedTextFieldCPF.getText().equals("")){ 符號:變數 jFormattedTextFieldCPF 位置:類 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 類中,我只有一個名為 funcoes 的函式,在我的資料庫上會有更多的操作要做,比如,編輯,添加,搜索......
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:01\n" ex.getMessage());
}
}
抱歉,這個問題很長,為了簡化,我想在另一個類中獲取有關 jFormattedTextField 的資訊。
uj5u.com熱心網友回復:
您可以宣告一個型別為 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
}
}
uj5u.com熱心網友回復:
解耦你的代碼。傳遞完成作業所需的所有資訊,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();
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/345727.html
