【问题标题】:Recording which array is chosen in an array of JButtons记录在 JButtons 数组中选择了哪个数组
【发布时间】:2018-08-21 09:19:30
【问题描述】:

我的麻烦在于试图记录选择了哪个数组。例如,如果我有一个包含 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 数组在程序开始时被打乱,以便在每次运行时使用不同的顺序。 What I want to know is when one of these JButtons is selected, how can I record which Array element was selected?所以我可以用相应的货币数组做一些事情。 (例如,选择了第 7 个 JButton,然后我想将 JButton 的文本更改为第 7 个货币数组中保存的数字。) 帮助将不胜感激。这是下周到期的,还有很多工作要做。

【问题讨论】:

  • 您需要展示更多代码和设置。在目前的状态下,这个问题太宽泛了。如果不反复讨论您的项目结构以及解决方案的具体外观,就无法回答这个问题。
  • 你如何确定一个按钮被选中了?你有一些事件监听器吗?每个按钮有一个还是一个?按钮知道它们的索引吗?听众知道索引吗?为什么不是每个按钮都与不同的操作相关联?与其索引对应的动作。
  • 添加了更多代码。对于那个很抱歉。稍后有一个事件侦听器,但是 atm 中没有任何内容(或者至少没有任何内容),因为我不知道在其中放入什么会产生所需的结果。另外,按钮/听众是否知道索引是什么意思?那会做什么,我该如何设置?
  • 嗯,通常你会将一个动作与每个按钮相关联。当一些被选中时,你有一个监听器响应事件。要么您有一个触发所有按钮的侦听器,要么每个按钮都有一个侦听器。后一种方案知道其对应的按钮,可以触发相应的动作。第一个解决方案需要迭代按钮并检查哪个被选中。然后根据所选按钮使用适当的操作。 general tutorial 可能比 StackOverflow 更有帮助。
  • 我有第一个听众。它将为所有按钮激活(因为窗口包含的唯一按钮是数组)。不过你说的基本上是我需要的。知道选择了哪个按钮以允许我在货币数组中执行相应的操作。

标签: java arrays jbutton


【解决方案1】:

我建议创建一个自定义按钮类,该类保存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]));
}

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 尝试使用此解决方案。按钮设置和执行的操作都处于公共空白中。但是在 actionPerformed 中找不到 caseButtons in 'int index = caseButtons.get(source);'
    猜你喜欢
    • 2016-02-20
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2020-10-08
    • 1970-01-01
    相关资源
    最近更新 更多