本文介绍了如果在class1中检查了复选框,在class2中检查了settext texfield?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我使用netbeans Jframe表单创建这些类。我在class1中放了一个复选框和一个按钮,在class2中放了一个textfield。 我想用class1将文本发送到class2的textfield。我没有收到错误,但代码不起作用。你能帮我解决一下吗?谢谢。 package test; public class class1 extends javax.swing.JFrame { public class1(){ initComponents(); } @ SuppressWarnings ( unchecked) // < editor-fold defaultstate =折叠desc =生成代码> private void initComponents()... public void 已发送(class2 theOtherClassInstance){ if (jCheckBox1.isSelected()){ theOtherClassInstance.jTextField1.setText( 那是); // class2中的这个jtextfield } } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){ class2 theOtherClassInstance = new class2();发送(theOtherClassInstance); 此 .setVisible(false); new class2()。setVisible(true); } public static void main( String args []){ java.awt.EventQueue.invokeLater( new Runnable(){ public void run(){ new class1()。setVisible(true); } }); } // 变量声明 - 不要修改 private javax.swing.JButton jButton1; private javax.swing.JCheckBox jCheckBox1; // 变量结束声明 } 这是class2: package test; public class class2 extends javax.swing.JFrame { public class2(){ initComponents(); } @ SuppressWarnings ( unchecked) // < editor-fold defaultstate =折叠desc =生成代码> private void initComponents()... public static void main( String args []){ java.awt.EventQueue.invokeLater( new Runnable(){ public void run(){ new class2()。setVisible(true); } }); } // 变量声明 - 不要修改 public javax.swing.JTextField jTextField1; // 变量结束声明 } 解决方案 猜测, jtextfield 被定义为类的实例成员classB ,但你试图像一个类( static )成员一样访问它。 这是来自VB.NET的用户的常见问题,因为VB.NET为您创建了 Form 类的默认实例,并使它们可用与类同名的全局变量。 (所以 Form1.TextBox1 访问 Form1 类的默认实例上的控件。)这是为了向后 - 兼容VB6,并且总是会让人们感到困惑,因为他们会转移到另一种语言! 您需要一个类的实例才能访问实例成员: public first(classB theOtherClassInstance){ initComponents(); if (jCheckBox1.isSelected()){ theOtherClassInstance.jtextfield.setText( 就是这样!); } } 注意:如果你发布错误总会有帮助你得到的消息。 :) I create these classes with netbeans Jframe form. I put a checkbox and a button in class1 and a textfield in class2.I want to sent a text to the textfield in class2 with class1. I don't recieve error but code isn't work. Could you help me for fix it? Thanks.package test;public class class1 extends javax.swing.JFrame {public class1() { initComponents();}@SuppressWarnings("unchecked")// <editor-fold defaultstate="collapsed" desc="Generated Code">private void initComponents()...public void sent(class2 theOtherClassInstance) { if (jCheckBox1.isSelected()) { theOtherClassInstance.jTextField1.setText("That's it"); // this jtextfield in the class2 }}private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { class2 theOtherClassInstance = new class2(); sent(theOtherClassInstance); this.setVisible(false); new class2().setVisible(true);}public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new class1().setVisible(true); } });}// Variables declaration - do not modifyprivate javax.swing.JButton jButton1;private javax.swing.JCheckBox jCheckBox1;// End of variables declaration}This is the class2:package test;public class class2 extends javax.swing.JFrame { public class2() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents()... public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new class2().setVisible(true); } }); } // Variables declaration - do not modify public javax.swing.JTextField jTextField1; // End of variables declaration} 解决方案 At a guess, jtextfield is defined as an instance member of the class classB, but you're trying to access it like a class (static) member.This is a common problem for users coming from VB.NET, since VB.NET creates a default instance of your Form classes for you, and makes them available as global variables with the same name as the class. (So Form1.TextBox1 accesses the control on the default instance of the Form1 class.) This was done for backwards-compatibility with VB6, and always causes confusion for people moving to a different language!You'll need an instance of the class in order to access the instance members:public first(classB theOtherClassInstance) { initComponents(); if (jCheckBox1.isSelected()) { theOtherClassInstance.jtextfield.setText("That's it !"); }}NB: It always helps if you post the error message you're getting. :) 这篇关于如果在class1中检查了复选框,在class2中检查了settext texfield?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-11 12:41