2017-12-05 5 views
-1

選択したすべてのJRadioButtonの名前を自分のグラフィックスインターフェイスから取得しようとしています。私はJRadioButtonsをすべて含んだallFacilities配列を作成します。JavaでJRadioButtonの名前を取得する

最初のforループは、選択されたラジオボタンの数を見つけるのに役立ちます。

2番目のforループは、選択した各ボタンの名前を取得します。

.getName()が返す値を確認すると、 System.out.println("A##" + button.getName());が返されます。すべてのケースでnullが返されます。ここで

は私のコードは次のとおりです。

JRadioButton[] allFacilities = {restaurant, laundry, parking}; 
    int selectedFacilitiesCounter = 0; 
    for(JRadioButton check : allFacilities) { 
     if(check.isSelected()) { 
      selectedFacilitiesCounter += 1; 
     } 
    } 
    String[] selectedFacilities = new String[selectedFacilitiesCounter]; 
    int index = 0; 
    for(JRadioButton button : allFacilities) { 
     if(button.isSelected()) { 
      System.out.println("A##" + button.getName()); 
      switch(button.getName()) { 
       case "restaurant": 
        selectedFacilities[index] = "restaurant"; 
        break; 
       case "laundry": 
        selectedFacilities[index] = "laundry"; 
        break; 
       case "parking": 
        selectedFacilities[index] = "parking"; 
        break; 
       default: 
        System.out.println("Facility Not Found"); 
      } 
      index += 1;   
     } 
    } 

誰もが、私は私の問題を解決することができる方法上の任意のアイデアを持っていますか? testを印刷します

JRadioButton button = new JRadioButton("test"); 
    System.out.println(button.getText()); 

+0

「名前」とは、表示されるテキストを意味しますか?あなたは 'getText()'を試しましたか?名前を扱うには、最初に 'setName(string)'を呼び出す必要があります。そして、後でそれを得ることができます。 – user1803551

+0

私はsetName()を試します。配列を初期化するときに表示されるものと同じJRadioButton変数の名前を意味します。 –

+0

代わりに 'button.getText()'を使用してください。 –

答えて

1

は、私が何をしたいことは、このであると信じています。

メソッドgetNameは、setNameと設定したはずのコンポーネントのnameを取得します。

関連する問題