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

我的麻烦来自试图记录正在选择的数组。例如,如果我有一个包含 5 个元素的数组,则记录选择了第三个元素。放在上下文中,我有 2 个不同的数组,每个数组有 10 个元素;一个数组由整数(称为money)组成,另一个是JButton 数组。


public class Game extends JFrame {


JPanel cases = new JPanel(new GridLayout(2, 5)); //Section containing game cases

JButton[] caseButton = new JButton[10];                                   // Declaration of the

String gameCase[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};  //    case buttons

int[] 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 个货币数组中保存的数字。)将不胜感激。这将在下周到期,还有很多工作要做。


喵喵时光机
浏览 123回答 2
2回答

UYOU

我建议创建一个自定义按钮类,用于保存money数组中相应值的索引。例如:public class MyButton extends JButton {&nbsp; &nbsp; private int moneyIndex;&nbsp; &nbsp; public MyButton(String text, int moneyIndex){&nbsp; &nbsp; &nbsp; &nbsp; super(text);&nbsp; &nbsp; &nbsp; &nbsp; this.moneyIndex = monexIndex;&nbsp; &nbsp; }&nbsp; &nbsp; public int getMoneyIndex(){&nbsp; &nbsp; &nbsp; &nbsp; return moneyIndex;&nbsp; &nbsp; }}然后,您可以使用与之前相同的方式创建一个按钮,但将货币索引传递给它:for (int i = 0; i < caseButton.length; i++) {&nbsp; &nbsp; // I suspect you want the moneyIndex to match the index of the button&nbsp; &nbsp; caseButton[i] = new MyButton("?", i);&nbsp; &nbsp; cases.add(caseButton[i]);&nbsp; &nbsp; // These can be moved to the custom button class if all MyButtons have these customizations&nbsp; &nbsp; caseButton[i].setPreferredSize(new Dimension(100, 100));&nbsp; &nbsp; caseButton[i].setFont(new Font("Dialog", Font.BOLD, 35));&nbsp; &nbsp; caseButton[i].setForeground(new Color(255, 215, 0));&nbsp; &nbsp; // Set this class as the action listener&nbsp; &nbsp; caseButton[i].setActionListener(this);}然后,在你的动作监听器中(我假设你的主类已经扩展了ActionListener),你可以访问 moneyIndex 变量:public void actionPerformed(ActionEvent e){&nbsp; &nbsp; // Get the source of the click as a MyButton&nbsp; &nbsp; MyButton source = (MyButton) e.getSource();&nbsp; &nbsp; // Get the moneyIndex of the source button&nbsp; &nbsp; int moneyIndex = source.getMoneyIndex();&nbsp; &nbsp; // Update the button's text according to the moneyIndex&nbsp; &nbsp; source.setText(Integer.toString(money[moneyIndex]));}这种方法的优点是索引由按钮存储,因此您不需要搜索所有按钮来检查哪个按钮被按下。随着您拥有的按钮数量的增加,这一点变得更加重要,但无论大小,都需要考虑这一点。此外,当这个项目变得更复杂时,这种方法会让你的生活更轻松,因为每个按钮都可以存储特定于它的信息,而无需一堆数组或操作命令。

蛊毒传说

作为自定义按钮类解决方案的替代方案,您可以将按钮保留在地图中,及其索引和操作处理程序上,根据源获取索引:Map<JButton, Integer> caseButtons = new HashMap<>(10);for(int i=0; i<10; i++) {&nbsp; &nbsp; JButton button = ...&nbsp; &nbsp; // all the other setup goes here&nbsp; &nbsp; caseButtons.put(button, i);}...public void actionPerformed(ActionEvent e){&nbsp; &nbsp; // Get the source of the click as a MyButton&nbsp; &nbsp; JButton source = (JButton) e.getSource();&nbsp; &nbsp; int index = caseButtons.get(source);&nbsp; &nbsp; ...}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java