Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JButton数组ActionListener_Java_Swing_Event Handling_Jbutton_Actionlistener - Fatal编程技术网

Java JButton数组ActionListener

Java JButton数组ActionListener,java,swing,event-handling,jbutton,actionlistener,Java,Swing,Event Handling,Jbutton,Actionlistener,简而言之,我想将一组JButton累积到一个数组中,并为数组创建一个ActionListener类 我正在尝试创建一个计算器,所有带编号的按钮,如“6”,都在JButton数组中,因为我希望它将设置的数字输入到一个临时int中,创建一个方法比创建10更容易。我还有40个其他按钮,我想对它们应用相同的原理,但在不同的数组中,因此将它们放入几个ActionListener方法中会更快更容易,在这些方法中,按钮数据被实现到该方法中。 这是我的代码: private JButton num0, num1

简而言之,我想将一组
JButton
累积到一个数组中,并为数组创建一个
ActionListener

我正在尝试创建一个计算器,所有带编号的按钮,如“6”,都在
JButton
数组中,因为我希望它将设置的数字输入到一个临时int中,创建一个方法比创建10更容易。我还有40个其他按钮,我想对它们应用相同的原理,但在不同的数组中,因此将它们放入几个
ActionListener
方法中会更快更容易,在这些方法中,按钮数据被实现到该方法中。 这是我的代码:

private JButton num0, num1, num2, num3, num4, num5, num6, num7, num8, num9;
private JButton numArray[] = {num0, num1, num2, num3, num4, num5, num6, num7, num8, num9};

        public GUI(){
          numArray.AddActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent event){

             }
       });
  }

你的想法是对的。但是,数组对象没有
addActionListener()
方法。您必须为每个
JButton
单独添加一个操作侦听器。您可以对每个按钮使用相同的侦听器,但是您必须确定在
actionPerformed()
方法中单击了哪个按钮。在我看来,一个更干净的解决方案是为每个JButton分配一个单独的侦听器,因为这样每个ActionListener就可以知道按下了哪个数字,而无需检查事件源。例如,您可以创建一个
NumberButtonListener
类,该类将
int
作为其构造函数的唯一参数。然后,您可以在一个小循环中同时创建JButtons和相应的
NumberButtonListener
s。

您的想法是正确的。但是,数组对象没有
addActionListener()
方法。您必须为每个
JButton
单独添加一个操作侦听器。您可以对每个按钮使用相同的侦听器,但是您必须确定在
actionPerformed()
方法中单击了哪个按钮。在我看来,一个更干净的解决方案是为每个JButton分配一个单独的侦听器,因为这样每个ActionListener就可以知道按下了哪个数字,而无需检查事件源。例如,您可以创建一个
NumberButtonListener
类,该类将
int
作为其构造函数的唯一参数。然后你可以在一个小循环中同时创建JButtons和相应的<代码>编号ButoListListNave/Cult>S。

< P>你可以考虑NeWB MUNAD的建议。但是,您可以对所有按钮使用相同的侦听器,如下例所示

public static void main(String[] args) {

    ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() instanceof JButton) {
                String text = ((JButton) e.getSource()).getText();
                JOptionPane.showMessageDialog(null, text);
            }
        }
    };

    JPanel panel = new JPanel(new GridLayout(4,3));

    JButton[] array = new JButton[10];
    for (int i = 0; i < array.length; i++) {
        array[i] = new JButton(String.valueOf(i));
        array[i].addActionListener(listener);
        panel.add(array[i]);
    }

    JOptionPane.showMessageDialog(null, panel);

}
publicstaticvoidmain(字符串[]args){
ActionListener=新建ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
if(例如,JButton的getSource()实例){
字符串文本=((JButton)e.getSource()).getText();
showMessageDialog(空,文本);
}
}
};
JPanel面板=新JPanel(新网格布局(4,3));
JButton[]数组=新JButton[10];
for(int i=0;i


< P>你可以考虑Neb B的建议。但是,您可以对所有按钮使用相同的侦听器,如下例所示

public static void main(String[] args) {

    ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() instanceof JButton) {
                String text = ((JButton) e.getSource()).getText();
                JOptionPane.showMessageDialog(null, text);
            }
        }
    };

    JPanel panel = new JPanel(new GridLayout(4,3));

    JButton[] array = new JButton[10];
    for (int i = 0; i < array.length; i++) {
        array[i] = new JButton(String.valueOf(i));
        array[i].addActionListener(listener);
        panel.add(array[i]);
    }

    JOptionPane.showMessageDialog(null, panel);

}
publicstaticvoidmain(字符串[]args){
ActionListener=新建ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
if(例如,JButton的getSource()实例){
字符串文本=((JButton)e.getSource()).getText();
showMessageDialog(空,文本);
}
}
};
JPanel面板=新JPanel(新网格布局(4,3));
JButton[]数组=新JButton[10];
for(int i=0;i


我在“连接四”游戏的2D按钮阵列上也遇到了类似的问题。我能够在ActionListener中使用for循环来测试我的哪些按钮被按下。键在修改button类中的toString()方法,以将数组元素作为字符串提供:

在JPanel类定义中:

public类RoundButton扩展了JButton{
...
公共字符串toString(){
返回“光盘[“+i+”][“+j+”]”;
}
...
}
私有类ButtonListener实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件){
对于(int i=0;i

抱歉,这太乱了。我以前从未在这里发布过。

我在“连接四”游戏的2D按钮数组中遇到类似问题。我能够使用ActionListener中的for循环来测试我的哪些按钮被按下。关键是修改了toString()方法将数组元素作为字符串提供:

在JPanel类定义中:

public类RoundButton扩展了JButton{
...
公共字符串toString(){
返回“光盘[“+i+”][“+j+”]”;
}
...
}
私有类ButtonListener实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件){
对于(int i=0;i

很抱歉,这太乱了。我以前从未在这里发布过。

您需要使用循环。您需要向我们报告
public class RoundButton extends JButton {
...
    public String toString() {
        return "discs["+i+"]["+j+"]";
    }
...
}

private class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent event) {

    for (int i = 0; i < discs.length; i++){
        for (int j= 0; j < discs[i].length; j++){
            if (event.getSource() == discs[i][j]){
                discs[i][j].setIcon(yellowDisc); //my particular action for that button
            }                                       
        }