2017-01-05 3 views
-1

現在、別のクラスの文字列値を変更する方法がわからない問題が発生しています。他のクラスの文字列値を変更する

このボタンがありますが、クリックした後、そのクラスで宣言されている別のクラスの文字列値を変更します。メインクラスで

、私はすでにオブジェクトを作成している:

public class BTRSMain { 
    private MainMenuPage mainmenupage; 
public void start() throws Exception{ 
    mainmenupage = new MainMenuPage(); 
}} 

次のコードは、ボタンの機能を示しています

public class OriginSubmit implements ActionListener { 
    public void actionPerformed(ActionEvent SignUpSubmit){ 
     if(mainmenupage.OriginCB.getSelectedItem().equals("APU")){ 
      //After clicking the button the string will change into: 
      //String[] Destination = ("Seremban","Melaka","Ipoh","Kuantan"); 
     } 
    } 
} 

別のクラス名が "MainMenuPage.java" です。コンボボックス内の文字列の文字列とインクルージョンの 宣言:

public class MainMenuPage { 
    String[] Destination; 
    JComboBox DestinationCB = new JComboBox(Destination); 
} 

任意の助けもいただければ幸いです。ありがとう!

+2

セッターとゲッターのコンセプトに慣れてください。 –

+0

これは基本を学ぶときに学ぶ非常に基本的な質問のようです。 Javaでのプログラミングの基本はどれくらいですか? – Popeye

+0

'throws Exception'を宣言するのは悪い考えです。特定のチェック例外があればそれを投げる。 –

答えて

0

これには2通りの方法があります。 OriginSubmitクラスにMainMenuPageクラスのオブジェクトを作成することでそれを行うことができます。

public class OriginSubmit implements ActionListener { 
MainMenuPage mainMenu = new MainMenuPage(); //this line of code to be added 

public void actionPerformed(ActionEvent SignUpSubmit){ 
    if(mainmenupage.OriginCB.getSelectedItem().equals("APU")){ 
     mainMenu.Destination = new String[]{"Seremban","Melaka","Ipoh","Kuantan"}; //line of code to be added 

     //After clicking the button the string will change into: 
     //String[] Destination = new String[]{"Seremban","Melaka","Ipoh","Kuantan"}; 
    } 
} 
} 

ここで2番目の方法について説明します。オブジェクトを必要としない場合は、MainMenuPageクラスを静的にする必要があります。その後、OriginSubmitクラスで使用します。

public static class MainMenuPage { //make it static 
String[] Destination; 
JComboBox DestinationCB = new JComboBox(Destination); 
} 

public class OriginSubmit implements ActionListener { 
public void actionPerformed(ActionEvent SignUpSubmit){ 
    if(mainmenupage.OriginCB.getSelectedItem().equals("APU")){ 
     MainMenuPage.Destination = new String[]{"Seremban","Melaka","Ipoh","Kuantan"}; //this line of code to be added 

     //After clicking the button the string will change into: 
     //String[] Destination = ("Seremban","Melaka","Ipoh","Kuantan"); 
    } 
} 
} 
+0

私は静的に変更すると、ここでは変更子の静的に許可されていません。 –

+0

そのクラスのオブジェクトを作成するだけで、Destination変数を変更できます。 –

+0

申し訳ありませんが、私はかなりJavaに新しいので、私は本当に理解していない、あなたは私を見せてくれますか? –

0

同じパッケージに、次のようにあなたのコメントコードを交換しているクラスMainMenuPageとOriginSubmitと仮定すると:

あなたのクラスが別々のパッケージに含まれている場合は
/*Construct the MainMenuPage object */ 
MainMenuPage mainMenuPage = new MainMenuPage(); 

/*Invoke the required method on the constructed object*/ 
mainMenuPage.Destination = new String[]{"Seremban","Melaka","Ipoh","Kuantan"}; 

、などpackage-privateメンバー( ' 1つのクラスからの「宛先」は別のクラスから直接アクセスすることはできません。あなたは外部の人がそれを呼び出すことができるように、公開のsetterメソッドをクラスに追加する必要があります。例えば。 public void setDestination(String[] newDestination){this.Destination = newDestination;}

+0

配列次元がありません。文字列をString []に変換することはできません。 –

+0

両方のクラスの完全なコードを表示する必要があります。ここで引用したエラーは、文字列配列にプレーンなStringオブジェクトを割り当てようとしていることを意味します。 – VHS

0

保存エンキャップ!クリーンなコード(変数の命名)に従ってください。

OOPとJavaの基礎を学びます。これはgetterとsetterを使用することによって達成されるため、基本的なものです。

public class MainMenuPage { 
    private String[] destination; 
    JComboBox destinationCB = new JComboBox(destination); 

    public MainMenupage() { 
     this.destination = new String[4]; 
    } 

    // Your other code 

    public void setDestination(String[] destination) { 
     this.destination = destination; 
    } 

    public String[] getDestination() { 
     return this.destination; 
    } 
} 

は、それからちょうどこのセッターを呼び出す:あなたはマルチレベルの継承と呼ばれて探している

public class OriginSubmit implements ActionListener { 
    public void actionPerformed(ActionEvent SignUpSubmit){ 
     if(mainmenupage.OriginCB.getSelectedItem().equals("APU")){ 
      // I dont know where are you declare object for MainMenu class 
      mainMenu.setDestination(/* your value */); 
     } 
    } 
} 
+0

このエラーは、実際の引数と仮引数の長さが異なります。 –

+0

自分のクラスに独自のコンストラクタを追加する必要があると思います。どうやって書いたのか分かりませんが、問題はあなたがコンストラクターを見逃していると思います。 Javaの基本を確認してください。 – Hrabosch

+0

http://www.homeandlearn.co.uk/java/class_constructor.html – Hrabosch

0

対象。キーワード「extend」では、クラスはもはや分離されず、システム全体のすべてのメソッドと変数からなる仮想スーパークラスを形成しています。マルチレベル継承は、迅速なアプリケーションプログラミングのための強力なツールです。

public class BTRSMain { 
    private MainMenuPage mainmenupage; 
public void start() throws Exception{ 
    mainmenupage = new MainMenuPage(); 
}} 
public class OriginSubmit implements ActionListener extends BTRSMain { 
    public void actionPerformed(ActionEvent SignUpSubmit){ 
     if(mainmenupage.OriginCB.getSelectedItem().equals("APU")){ 
      //After clicking the button the string will change into: 
      //String[] Destination = ("Seremban","Melaka","Ipoh","Kuantan"); 
     } 
    } 
} 
public class MainMenuPage extends OriginSubmit { 
    String[] Destination; 
    JComboBox DestinationCB = new JComboBox(Destination); 
} 
関連する問題