【问题标题】:Return the index of clicked button?返回点击按钮的索引?
【发布时间】:2012-05-12 01:35:38
【问题描述】:

我有一组 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?
    }

}
}

【问题讨论】:

  • 给我们看一些代码。我们不是介意读者。你在用swing吗?
  • 你可以在this链接查看Java keypress API
  • ButtonClickEvents 不适用于这种情况? (按这 30 个按钮之一还是按另一个按钮?)
  • @phantasmagoria 按下这 30 个按钮之一。检查我上面的代码

标签: java arrays swing jbutton


【解决方案1】:

我更喜欢 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..

【讨论】:

  • 请查看修改后的版本。如何在 PositionLanding 索引上设置 imageIcon?它给出了一个 int cannot be derefernced 错误。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    最漂亮的方法是使用 Component.setName。然后你甚至不需要在你的组件中维护变量——你可以直接使用名称

    【讨论】:

      【解决方案4】:

      您可以在ActionEvent.getSource() 中找到该按钮。要找到索引,只需遍历数组,寻找那个特定的按钮。

      【讨论】:

        猜你喜欢
        • 2014-10-05
        • 2016-10-24
        • 1970-01-01
        • 2013-06-25
        • 1970-01-01
        • 2015-06-22
        • 1970-01-01
        • 1970-01-01
        • 2023-01-29
        相关资源
        最近更新 更多