【问题标题】:How to get data from a jTextField or jFormattedTextField in another java class如何从另一个 java 类中的 jTextField 或 jFormattedTextField 获取数据
【发布时间】:2021-12-05 14:30:06
【问题描述】:

我正在尝试使用 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 的信息。

【问题讨论】:

  • 错误很明显,jFormattedTextFieldCPF 在类Modos 中未定义。问题是非常基本的 Java 101。您需要从其他类传递您的类/方法所需的信息
  • 好的,怎么做?我还需要在所有摆动元素中做同样的事情
  • 更改funcoes 以接受它需要执行的输入

标签: java swing class jtextfield jformattedtextfield


【解决方案1】:

你可以声明一个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 
  }
  }

【讨论】:

  • Doesn't work 据说错误在这里:` public void funcoes jFormattedTextFieldCPF (int MODO) { jTextField recievedjFormattedTextFieldCPF = Pessoa.getjToPass();` error: '(' expected public void funcoes jFormattedTextFieldCPF (int MODO) {
  • public void funcoes jFormattedTextFieldCPF stackoverflow.com/questions/15029480/…
【解决方案2】:

解耦您的代码。传递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();
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 2015-03-11
    • 1970-01-01
    • 2012-09-26
    相关资源
    最近更新 更多