为了账号安全,请及时绑定邮箱和手机立即绑定

记录在 JButton 数组中选择了哪个数组

记录在 JButton 数组中选择了哪个数组

喵喵时光机 2021-08-04 10:31:04
我的麻烦来自试图记录正在选择的数组。例如,如果我有一个包含 5 个元素的数组,则记录选择了第三个元素。放在上下文中,我有 2 个不同的数组,每个数组有 10 个元素;一个数组由整数(称为money)组成,另一个是JButton 数组。public class Game extends JFrame {JPanel cases = new JPanel(new GridLayout(2, 5)); //Section containing game casesJButton[] caseButton = new JButton[10];                                   // Declaration of theString gameCase[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};  //    case buttonsint[] money = {1, 2, 5, 10, 100, 1000, 5000, 10000, 20000, 30000}; //money values}public void StartGame() {    Shuffle(money); //Shuffle the money    //Initialising case buttons    for (int i = 0; i < caseButton.length; i++) {        caseButton[i] = new JButton(gameCase[i]);        cases.add(caseButton[i]);        caseButton[i].setPreferredSize(new Dimension(100, 100));        caseButton[i].setFont(new Font("Dialog", Font.BOLD, 35));        caseButton[i].setForeground(new Color(255, 215, 0));        caseButton[i].setActionCommand(gameCase[i]);    }Money 数组在程序开始时被打乱,以允许每次运行时使用不同的顺序。我想知道的是选择其中一个jbuttons时,如何如何录制选择哪个数组元素?所以我可以用相应的钱数组做一些事情。(例如,选择了第 7 个 JButton,然后我想将 JButton 的文本更改为第 7 个货币数组中保存的数字。)将不胜感激。这将在下周到期,还有很多工作要做。
查看完整描述

2 回答

?
UYOU

TA贡献1878条经验 获得超4个赞

我建议创建一个自定义按钮类,用于保存money数组中相应值的索引。例如:


public class MyButton extends JButton {

    private int moneyIndex;


    public MyButton(String text, int moneyIndex){

        super(text);

        this.moneyIndex = monexIndex;

    }


    public int getMoneyIndex(){

        return moneyIndex;

    }

}

然后,您可以使用与之前相同的方式创建一个按钮,但将货币索引传递给它:


for (int i = 0; i < caseButton.length; i++) {

    // I suspect you want the moneyIndex to match the index of the button

    caseButton[i] = new MyButton("?", i);


    cases.add(caseButton[i]);


    // These can be moved to the custom button class if all MyButtons have these customizations

    caseButton[i].setPreferredSize(new Dimension(100, 100));

    caseButton[i].setFont(new Font("Dialog", Font.BOLD, 35));

    caseButton[i].setForeground(new Color(255, 215, 0));


    // Set this class as the action listener

    caseButton[i].setActionListener(this);

}

然后,在你的动作监听器中(我假设你的主类已经扩展了ActionListener),你可以访问 moneyIndex 变量:


public void actionPerformed(ActionEvent e){

    // Get the source of the click as a MyButton

    MyButton source = (MyButton) e.getSource();


    // Get the moneyIndex of the source button

    int moneyIndex = source.getMoneyIndex();


    // Update the button's text according to the moneyIndex

    source.setText(Integer.toString(money[moneyIndex]));

}

这种方法的优点是索引由按钮存储,因此您不需要搜索所有按钮来检查哪个按钮被按下。随着您拥有的按钮数量的增加,这一点变得更加重要,但无论大小,都需要考虑这一点。


此外,当这个项目变得更复杂时,这种方法会让你的生活更轻松,因为每个按钮都可以存储特定于它的信息,而无需一堆数组或操作命令。


查看完整回答
反对 回复 2021-08-04
?
蛊毒传说

TA贡献1895条经验 获得超3个赞

作为自定义按钮类解决方案的替代方案,您可以将按钮保留在地图中,及其索引和操作处理程序上,根据源获取索引:


Map<JButton, Integer> caseButtons = new HashMap<>(10);

for(int i=0; i<10; i++) {

    JButton button = ...

    // all the other setup goes here

    caseButtons.put(button, i);

}

...


public void actionPerformed(ActionEvent e){

    // Get the source of the click as a MyButton

    JButton source = (JButton) e.getSource();

    int index = caseButtons.get(source);

    ...

}


查看完整回答
反对 回复 2021-08-04
  • 2 回答
  • 0 关注
  • 122 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信