2016-05-12 5 views
0

私はNetBeans 8.0.2を使用しています。私は現在の状況を解決するためにいくつかのフォーラムを検索しました。クラス内の別のパッケージのメソッドにアクセスできません。私は必要なパッケージをインポートし、クラスのインスタンスを作成しようとしました。まず、NetBeansは、私がinstanceName.methodName形式を使用してメソッドにアクセスしようとすると、パッケージが存在しないため、異なるパッケージのクラスに値を送信できません

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

次のことが可能なメソッドのリストが表示されないことを言って、コードの下に赤い線を置き、それだけで新しい示しています。 '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); 
    } 
} 

は今、私はIDEを残している給与計算クラス

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); 
    } 
} 

は、コードを生成しました。

+0

クラス外に文を配置することはできません。クラス宣言が完全に欠落しています。クラスを追加して、初期化文をイニシャライザ・ブロックまたはコンストラクタに入れます。https://docs.oracle.com/javase/tutorial/java/TOC.html –

+0

@AndreasFesterを参照していただきありがとうございます。 。私のコードは本当に長いので、問題を迅速かつ正確に解決できるように、関連する部分のみを取り出してください。もちろん、クラスは自分のコードで宣言されています。また、クラス外のコメントはありません。私は誤解を避けるために投稿したものを編集します。また、あなたの提案は私が試したことの一つであり、うまくいきませんでした。 – user3650467

+0

コンパイルされないものは除外しないでください。ただし、[MVCE](http://stackoverflow.com/help/mcve)を提供してください。ここに欠けている部分は、Cです。 –

答えて

1

このエラーは、calcDisp()をブロックの外に呼び出すという事実に起因します。 (推奨)コンストラクタを使用して

  1. はあなたのコードの作業を持っているいくつかの方法があります。これはあなたの現在の問題を修正する必要があります初期化ブロック

    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); 
        } 
    } 
    

を使用して

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); 
    } 
} 
  • 。私は残りのコードについてもっと多くの予備を持っています。あなたが省略したものは正しいと思います。なぜなら、コードは何もしないからです。

  • +0

    これはうまくいくかもしれませんが、私は間違いを認識しました。これで、Start2クラスのユーザーから受け取った値がPayRollクラスのJtextAreaに設定されていない状態になりました。私はあなたの提案を感謝します。 – user3650467

    0

    は アクセスその方法とは逆のパッケージBの輸入パッケージAに、そのメソッドにアクセスするために、パッケージAの輸入パッケージB のように、クロス輸入を行うことが可能です。

    まず、このアプローチは示唆されていません。セキュリティ上の問題を引き起こす可能性があります。 (See details)

    第二に、あなたはStart2クラスの内部クラスPayRollのインスタンスを作成し、相互にStart2クラス内部PayRollクラスのインスタンスを作成します。それはもう一つの間違ったアプローチです。

    あなたの状況についての解決策を得るには_Design Pattern_s(特にオブザーバーパターン)が必要です。

    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(); 
    
    } 
    } 
    

    給与計算クラスでcalcDisp()をコールする必要はありません給与クラス

    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); 
    } 
    } 
    

    :これはそれである上だから、

    は値を送信しますStart2のボタンがそれを処理します。他の変数を宣言して設定し、PayRollに送信することもできます。問題が解決しました。

    関連する問題