Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从带有“静态”变量的文本框中获取文本_Java_Sql_Swing_Jtextfield - Fatal编程技术网

Java 从带有“静态”变量的文本框中获取文本

Java 从带有“静态”变量的文本框中获取文本,java,sql,swing,jtextfield,Java,Sql,Swing,Jtextfield,我想从textfield contractNo获取文本。当前值是从另一个类导入的。但是,当我根据textfield contractNo的内容设置字符串变量contract的值时,错误状态为:不能从静态上下文引用非静态变量contract,不能从静态上下文引用非静态变量contractNo 代码如下: public static void main(final String user, final String cNo) { /* Set the Nimbus look and feel

我想从textfield contractNo获取文本。当前值是从另一个类导入的。但是,当我根据textfield contractNo的内容设置字符串变量contract的值时,错误状态为:不能从静态上下文引用非静态变量contract,不能从静态上下文引用非静态变量contractNo

代码如下:

public static void main(final String user, final String cNo) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(EditContract.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(EditContract.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(EditContract.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(EditContract.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */


    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            EditContract editC = new EditContract();
            editC.contractNo.setText(cNo);
            editC.encoder.setText(user);
            editC.setVisible(true);

            fillData();

        }
         private void fillData() {

            try {


                contract = contractNo.getText();
                String sql = "Select engager, contactInfo, eventDate, eventtime, address, menu, contract, referred "
                        + "from kusinanikambal.contracts where contractno = 1234";
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(sql);
            }
                catch(SQLException ex){
                ex.printStackTrace();
            }


        }


    });
}

您的代码已损坏,需要进行几个重要的修复

无论怎样,都不应尝试直接访问类字段。如果需要获取类字段的状态,则对该类的可行实例调用公共方法。在这里,我猜它可能是EditContract对象的一个字段,如果是的话,给EditContract一个公开其JTextField内容的公共方法。 如果EditContract不是模式对话框,即使您可以正确获取字段,您的代码也不会工作,因为在用户有机会输入任何内容之前,您会在显示GUI时立即提取数据。您似乎忽略了Java GUI是事件驱动的,这意味着您应该在事件中从GUI提取信息,通常是菜单或按钮的ActionListener。换句话说,您的fillData方法闻起来好像不属于您放置它的位置。 编辑 您在评论中声明:


Filldata实际上应该在程序初始化时执行

如果这是真的,那么contractNo JTextField应该保留什么值,因为该方法是在用户与程序交互之前运行的? 如果它需要在程序开始时运行,为什么不把它放在EditContract的构造函数中呢? 编辑2

为了澄清这一点,如果我的GUI类有一个JTextField,并且我需要从其他地方访问它,我会做如下操作:

public class MyGui {
  private JTextField myField = new JTextField(10);

  public String getFieldText() {
    return myField.getText();
  }

  // ....

}

然后,其他类可以在MyGui实例上调用getFieldText方法(如果它们需要访问文本)。

Filldata实际上应该在程序初始化时执行。我实现了您的建议,但它仍然显示相同的错误。@user3209957:那么您仍然以静态方式调用它!不要这样做-调用在对象上创建的public方法。您最好阅读Java入门书籍的前1-3章,了解静态和实例。如果需要进一步的帮助,那么考虑将最新的代码添加到底部,并添加任何和所有的完全错误消息。@ USER990057:同样,您是否按照我的建议调用类的构造函数中的代码?另外,您能否提供更多的细节,说明您试图解决的问题是什么——代码应该做什么?