2012-05-02 111 views
1

我有30個按鈕[]數組。我有一個變量buttonClicked。當我按下按鈕時,如何獲得索引並將索引號存儲在buttonClicked中?返回點擊按鈕的索引?

謝謝:)

JButton [] buttons = new JButton[30]; 


     for(int i = 1; i <= 30; i++) 
     {  
      int btnNumber = (i > 10 && i <= 20) ? (31 - i) : i; 

      System.out.printf("i = %d, btnNumber = %d%n", i, btnNumber); 
      buttons[btnNumber - 1] = new JButton("label " + btnNumber); 
      //buttons[btnNumber - 1].setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
      buttons[btnNumber - 1].setBorder(BorderFactory.createEtchedBorder()); 
      buttons[btnNumber - 1].setOpaque(true); 
      buttons[btnNumber - 1].setBackground(Color.white); 

      //Puts the player 1 piece on button 1,3,5,7,9 and player 2 piece on button 2,4,6,8,10 
      if ((btnNumber - 1) < 10) 
      { 
       if (((btnNumber - 1) % 2) == 0) 
       { 
        buttons[btnNumber - 1].setIcon(piece1); 
       } 
       else 
       { 
        buttons[btnNumber - 1].setIcon(piece2); 
       } 
      } 
      centerPanel.add(buttons[btnNumber - 1]); 
     } 

//下面是什麼,我試圖做的,我知道是不正確的。

public void move() 
{ 
Move = dice.getDiceResult(); 
int buttonClicked = 0; 

if(playerOneTurn =true) 
{ 
buttonclicked + diceResult(); 
} 

//修訂

public class MyActionListener implements ActionListener { 
Dice dice; 
private boolean playerOneTurn = true; 
private boolean playerTwoTurn = false; 
    @Override 
    public void actionPerformed(ActionEvent e) 
{ 
    String num = e.getActionCommand(); 
    int index = Integer.parseInt(num); 
    int move = dice.getDiceResult(); 
    int positionLanding = 0; 

    if(playerOneTurn = true) 
    { 
     positionLanding = index + move; 
     positionLanding.setIcon("piece1");//how can I set the image Icon to this position? 
    } 

} 
} 
+4

我們看一些代碼。我們不介意讀者。你在使用[tag:swing]嗎? –

+0

你可以看一下在[這](http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html)鏈接 – AurA

+0

ButtonClickEvents了Java API按鍵對於這種情況下無法正常工作? (這30個按鍵的按下一個或按另一個按鈕?) – phantasmagoria

回答

2

可以在ActionEvent.getSource()找到按鈕。要找到索引,只需要遍歷數組,尋找特定的按鈕。

+0

@ user1329572這也適用於AWT。 –

+0

@AndrewThompson,我知道。 – user1329572

2

最漂亮的方法是使用Component.setName。然後,你甚至都不需要維護變量與你的組件 - 你可以直奔名稱

4

1)putClientProperty

buttons[i][j].putClientProperty("column", i); 
buttons[i][j].putClientProperty("row", j); 
buttons[i][j].addActionListener(new MyActionListener()); 

getClientProperty

public class MyActionListener implements ActionListener { 

@Override 
public void actionPerformed(ActionEvent e) { 
    JButton btn = (JButton) e.getSource(); 
    System.out.println("clicked column " + btn.getClientProperty("column") 
      + ", row " + btn.getClientProperty("row")); 
} 

2)ActionCommand

的關
2

我更喜歡通過aioobe建議的策略,但在這裏是另一種方式。

buttons[btnNumber - 1] = new JButton("label " + btnNumber); 
buttons[btnNumber - 1].setActionCommand("" + btnNumber); 
// ... 

// ...later.. in the actionPerformed() method 
String num = actionEvent.getActionCommand(); 
int index = Integer.parseInt(num); 
// ..proceed.. 
+0

請檢查修訂版本。如何在PositionLanding索引上設置imageIcon?它是一個int不能被取消錯誤。 –

+0

*「請檢查修訂版本」*請發佈[SSCCE](http://sscce.org/)。 –