无法将值发送到不同包中的类,因为包不存在

时间:2016-05-12 08:19:22

标签: java netbeans pass-by-reference

我正在使用NetBeans 8.0.2。我搜索了几个论坛来解决我目前的情况。我无法访问类中不同包的方法。我已导入所需的包并尝试创建该类的实例。首先,NetBeans在代码下面加了一行红线

"Package sts2 does not exist. <identifier> expected"

接下来,当我尝试使用instanceName.methodName格式访问该方法时,它不显示可用方法的列表,它只显示新的。 'sts2'是我为'Start2'类创建的实例的名称,'Start2'类位于与使用检索到的输入的类不同的包中。我已经使用import语句导入了所需的包。

我尝试重新启动NetBeans,重新启动计算机,但它仍然是一样的。基本上我要做的是收集变量的值并将它们从一个类传递到另一个类并使用这些值进行计算。这些类是必须处理用户输入的GUI。

请注意我已经看过几篇关于在课程之间传递变量的帖子,但这并不符合我目前的困境。是否可以进行交叉导入,如包A导入包B以访问其方法然后反向包B导入包A以访问其方法。我在这做错了什么?我是否按照自己的意愿传递价值?

为了进一步清楚,请参阅下面的代码(忽略我没有使用com.name.barewithme类型的标准包名称)。我希望通过这项任务增强我对JavaSE的知识和技能。

检索和发送数据的Start2类。

package home;
import Solution_screens.*;

public class Start2 extends javax.swing.JFrame {
  //this is the Start2 class it recieves user input via GUI then sends to PayRoll class in different package ie Solutions_screens)
  public void calcDisp(){
    //this method is called from PayRoll class. it collects and sends the values over
    String firstname = jTextField1.getText();
    String sal1 = jTextField5.getText();
    float sal = Integer.parseInt(sal1);
    PayRoll p = new PayRoll(); //creating instance of the PayROll class so I can send retrieved values over there
    p.userInput(firstname); //userInput method is defined in PayRoll class
  }
  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // this button was dragged in using NB DnD

    PayRoll payr = new PayRoll(); //another instance of PayRoll to make it visible
    payr.setVisible(true);
  }
}

现在是PayRoll类

package Solution_screens;
import home.Start2;

public class PayRoll extends javax.swing.JFrame {
  //here I only need the Start2 class and dont have to import all the classes of the package
  Start2 sts2=new Start2(); //creating an instance of Start2 so I can access the calcDisp() method. It highlights sts2 to green, which should not be.
  sts2.calcDisp(); // this is where the problem seems to be. NB gives the error that it does not exist
  // this method is created to collect the values and work with them.
  public void userInput(String name){
    jTextArea1.setText(name);
  }
}

我遗漏了IDE生成的代码,因为它们似乎工作正常。

3 个答案:

答案 0 :(得分:1)

错误来自于您在任何区域之外调用calcDisp()这一事实。

有几种方法可以使您的代码正常工作:

  1. 使用构造函数(推荐)。

    package Solution_screens;
    import home.Start2;
    
    public class PayRoll extends javax.swing.JFrame {
      Start2 sts2;
      public PayRoll() {
        sts2 = new Start2();
        sts2.calcDisp();
      }
      public void userInput(String name){
        jTextArea1.setText(name);
      }
    }
    
  2. 使用初始化块

    package Solution_screens;
    import home.Start2;
    
    public class PayRoll extends javax.swing.JFrame {
      Start2 sts2;
      {
        sts2 = new Start2();
        sts2.calcDisp();
      }
      public void userInput(String name){
        jTextArea1.setText(name);
      }
    }
    
  3. 这应该可以解决您当前的问题。我对其余的代码有更多的保留,并希望你省略的内容是正确的,因为实际上,你的代码不会做任何事情。

答案 1 :(得分:0)

  

是否可以进行交叉导入,例如包A导入包B.   然后反向访问它的方法包B将包A导入到   访问它的方法。

首先,不建议采用这种方法。它可能会导致安全问题。 (See details)

其次,在PayRoll类中创建类Start2的实例,并在Start2类内创建PayRoll类的实例。这是另一种错误的做法。

您必须查看_Design Pattern_s(尤其是 Observer Pattern )以获得有关您情况的解决方案。

答案 2 :(得分:0)

我感谢对此主题的关注。从最初的问题开始,我的代码是在一个块之外,因此我无法访问任何方法,并且NetBeans无法向我提供除了显示的新建议之外的建议。

所以要通过它发送值是:

Start2课程

package home;
import Solution_screens.*;

public class Start2 extends javax.swing.JFrame {
//this is the Start2 class it recieves user input via GUI then sends to PayRoll class in different package ie Solutions_screens)
public void calcDisp(){
//this method is called from here using button below. it collects and sends the values over
String firstname = jTextField1.getText();
String sal1 = jTextField5.getText();
float sal = Integer.parseInt(sal1);
PayRoll p = new PayRoll(); //creating instance of the PayROll class so I can send retrieved values over there
p.userInput(firstname, sal); //userInput method is defined in PayRoll class
p.setVisible(true);//use same instance to show the PayRoll GUI
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{                                         
// this button was dragged in using NB DnD
//no need creating another instance of PayRoll class just call the calcDisp method created above and it sends users retreived data over.

calcDisp();

}
}

现在是PayRoll类

package Solution_screens;
import home.Start2;

public class PayRoll extends javax.swing.JFrame {
//here I only need the Start2 class and dont have to import all the classes of the package


// this method is created to collect the values and work with them.
public void userInput(String name, String ssal){
jTextArea1.setText("Payroll Calculation for: " +name +"\t" "Salary is : "+ssal);
}
}

无需在PayRoll Class中调用calcDisp(),Start2中的按钮处理它。我甚至可以声明并设置其他变量并将它们发送到PayRoll。问题解决了。