返回点击按钮的索引? [英] Return the index of clicked button?

查看:127
本文介绍了返回点击按钮的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有30个按钮[]数组。我有一个变量buttonClicked。当我preSS的按钮,我怎么能得到索引和存储在buttonClicked指数是多少?

感谢:)

  JButton的[]按钮=新的JButton [30];
        的for(int i = 1; I< = 30;我++)
        {
            INT btnNumber =(ⅰ大于10&安培;&放大器; I&下; = 20)? (31 - I):我;            System.out.printf(I =%d个,btnNumber =%D%N,我,btnNumber);
            按钮[btnNumber - 1] =的新的JButton(标签+ btnNumber);
            //键[btnNumber - 1] .setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
            按钮[btnNumber - 1] .setBorder(BorderFactory.createEtchedBorder());
            按钮[btnNumber - 1] .setOpaque(真);
            按钮[btnNumber - 1] .setBackground(Color.white);            //会将播放器1个按钮1,3,5,7,9和播放器2件按钮2,4,6,8,10
            如果((btnNumber - 1) - ; 10)
            {
                如果(((btnNumber - 1)%2)== 0)
                {
                    按钮[btnNumber - 1] .setIcon(piece1);
                }
                其他
                {
                    按钮[btnNumber - 1] .setIcon(piece2);
                }
            }
            centerPanel.add(按钮[btnNumber - 1]);
        }

//下面是什么,我试图做的,我知道是不正确的。

 公共无效移动()
{
动= dice.getDiceResult();
INT buttonClicked = 0;如果(playerOneTurn =真)
{
buttonclicked + diceResult();
}

//修改

 公共类MyActionListener实现的ActionListener {
骰子骰子;
私人布尔playerOneTurn = TRUE;
私人布尔playerTwoTurn = FALSE;
    @覆盖
    公共无效的actionPerformed(ActionEvent的五)
{
    字符串NUM = e.getActionCommand();
    INT指数=的Integer.parseInt(NUM);
    INT此举= dice.getDiceResult();
    INT positionLanding = 0;    如果(playerOneTurn =真)
    {
        positionLanding =指数+移动;
        positionLanding.setIcon(piece1); //我怎么可以设置图像图标这个位置吗?
    }}
}


解决方案

1)<一个href=\"http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#putClientProperty%28java.lang.Object,%20java.lang.Object%29\"相对=nofollow> putClientProperty

 按钮[i] [j]的.putClientProperty(列,我);
按钮[i] [j]的.putClientProperty(行,J);
按钮[i] [j]的.addActionListener(新MyActionListener());

和<一个href=\"http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#getClientProperty%28java.lang.Object%29\"相对=nofollow> getClientProperty

 公共类MyActionListener实现的ActionListener {@覆盖
公共无效的actionPerformed(ActionEvent的五){
    JButton的BTN =(JButton的)e.getSource();
    的System.out.println(栏点击+ btn.getClientProperty(列)
            +,行+ btn.getClientProperty(行));
}

2) ActionCommand

I have got an array of 30 buttons []. I have a variable buttonClicked. When I press the button how can I get the index and store the index number in the buttonClicked?

Thanks :)

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]); 
        } 

//Below is what I am attempting to do, I know is not correct.

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

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

//revised

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?
    }

}
}

解决方案

1) putClientProperty

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

and 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

这篇关于返回点击按钮的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆