1

jTextFieldのテキストを他のクラスから変更するにはどうすればよいですか?

クラスAがあり、アイテムを選択して[アカウントの作成]をクリックするとします。jTabbedPaneに同じ名前のタブを追加しました。このタブはクラスBです。このためのコードは次のとおりです。

「アカウントの作成」をクリックすると、この関数addclass(mainCB.getSelectedIndex())が呼び出されます

 public void addclass(int a) {
    String s=(String) mainCB.getItemAt(a); //mainCB is variable name of combobox
    JComponent subpanel2=new B(); //added the class
    jTabbedPane1.add(s,subpanel2); //added new tab which is the new class
    B ob=new B(); //object of new class B
    ob.heading(s); //heading is the function in Class B
}

では、jTextField1テキストをクラスAから変更するにはどうすればよいですか。

クラスBのheading()関数は次のとおりです。

public void heading(String s){
    head.setText(s); //head is the variable name of jTextField1 of class B
}

クラスAとクラスBの両方の画像を投稿しました。

これはクラスAです クラスA


jTabbedPaneに追加された新しいパネルはクラスBです。これはクラスAで呼び出されています。

クラスB

4

1 に答える 1

2

メソッドにクラスの2つのインスタンスを作成BしますaddClass。タイプの、を呼び出すことheadingで問題が解決すると思います。これは次のようになります。subpanel2B

public void addclass(int a) {
    String s=(String) mainCB.getItemAt(a); //mainCB is variable name of combobox
    B subpanel2=new B(); //added the class
    jTabbedPane1.add(s,subpanel2); //added new tab which is the new class
    subpanel2.heading(s); //heading is the function in Class B
}

これはあなたが望んでいたことですか?

于 2011-07-10T02:20:01.993 に答える