2010-03-12 90 views
3

使用本網站很多,但第一次發佈。 我的程序根據文件中記錄的數量創建多個按鈕。 例如5個記錄,5個按鈕。對循環中動態創建的每個按鈕有不同的操作

按鈕正在創建,但我有一個動作偵聽器的問題。

如果在循環中添加動作偵聽器,每個按鈕都會執行相同的操作;但是如果我在循環外部添加動作偵聽器,它只是將動作偵聽器添加到最後一個按鈕。

任何想法?

這裏是我的代碼明智的(我剛加入的for循環,以節省空間):

int j=0; 
for(int i=0; i<namesA.size(); i++) 
{ 
    b = new JButton(""+namesA.get(i)+""); 
    conPanel.add(b); 
    conFrame.add(conPanel); 

    b.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent ae2){ 

       System.out.println(namesA.get(j)); 

     } 
    }}); 
    j++; 
} 

大加讚賞

回答

2

當你正在爲你創建的每個按鈕創建一個動作監聽器,你可以有此:

final int buttonIndex = i; 
b.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent ae2) { 
     System.out.println("Button pressed is: " + buttonIndex); 
    } 
} 

要訪問匿名類方法中的變量,它必須標記爲final。這就是你有這個final int buttonIndex = i;聲明。

您可以使用按鈕上的setActionCommand方法來定義您可以從ActionEvent類的actionCommand屬性中檢索的操作命令。通過這樣做,您可以爲所有按鈕設置相同的偵聽器。您可以將該操作命令設置爲我在示例中定義的buttonIndex變量。通過這樣做,您可以在應用程序中創建更少的匿名類,這總是很好(少用消耗較少內存的對象)。

+0

這工程就像一個享受!非常感謝你。 – Oliver 2010-03-12 02:55:30

1

爲什麼不設置你的ActionListeners外循環並作出它們的數組,其中listeners數組中actionListener的索引與其添加到的按鈕相對應。類似這樣的:

ActionAdapter[] listeners = new ActionAdapter[namesA.size()]; 
//fill listeners with ActionAdapters 
listeners[0] = new ActionAdapter() 
{ 
    public void actionPerformed(ActionEvent e) { 
     //Do stuff 
    } 
}; 
//Repeat for each button you need 

for(int i = 0; i < namesA.size(); i++) 
{ 
    b = new JButton("" + namesA.get(i) + ""); 
    conPanel.add(b); 
    b.addActionListener(listeners[i]); 
} 

雖然警告,但我還沒有測試過這段代碼。

+0

這很好。謝謝,但我仍然有一個提交聽衆的問題,因爲它是未知的需要多少個按鈕...可能是5 ..可能是100 – Oliver 2010-03-12 02:38:41

2

您可以在創建按鈕時將每個按鈕的按鈕引用和索引(i)添加到哈希映射中。

在你的一個動作偵聽器中,你可以通過它的按鈕引用來查找在你的散列表中找到事件的按鈕的索引。

是這樣的(僞代碼,所以請不要downvote我,如果它不編譯):

Hashmap<JButton, Integer> map = new Hashmap<JButton, Integer>(); 

int j=0; 
for (int i = 0; i < namesA.size(); i++) 
{ 
    b = new JButton("" + namesA.get(i) + ""); 
    conPanel.add(b); 
    conFrame.add(conPanel); 

    // Add a mapping 
    map.add(b, new Integer(i)); 

    b.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent ae2) { 
      // Look up the button in the map, and get it's index 
      Integer index = map.get(ae2.getSource()); 

      // Do something different here based upon index 
     } 
    }); 
    j++; 
} 
+0

這是最好的一個謝謝 – 2012-08-26 18:17:53

0

您的第一個問題依賴於變量j

您將所有按鈕分配給完全相同的ActionListener,它將打印索引爲j的對象,該對象在顯示按鈕時是==增量時列表的最後一個索引,列表namesA

0
public class Scroll_view extends Activity { 

Button btn; 
Button btn1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_scroll_view); 



     LinearLayout linear=(LinearLayout)findViewById(R.id.linear); 
     for(int i=1; i<=20 ;i++){ 
      LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); 
      btn=new Button(this); 
      btn.setId(i); 
      final int id_=btn.getId(); 
      btn.setText("button " + id_); 
      linear.addView(btn,params); 


      btn1=((Button)findViewById(id_)); 
      btn1.setOnClickListener(new View.OnClickListener(){   
       public void onClick(View view){ 
        Toast.makeText(view.getContext() , "Button clicked index = " + id_ , Toast.LENGTH_SHORT).show(); 
       } 
       }); 
       } 
      } 
} 
+0

這個(android)的答案如何與op相關,誰不需要android解決方案? – oers 2012-09-25 10:43:29

相關問題