[英]How to get data from a jTextField or jFormattedTextField in another java class
我正在嘗試使用 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 的信息。
您可以聲明一個類型為 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
所需的所有信息,並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();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.