JButton 数组的操作侦听器

发布于 2024-10-10 06:04:29 字数 1249 浏览 0 评论 0原文

假设我有一个带有 2D 按钮数组的程序,当您单击其中一个按钮时,它会变成红色。我不想单独声明每个按钮,所以我只是为它们创建了 JButton[][] 数组。问题是我不知道如何在数组中的任何按钮上使用操作侦听器,因此它会更改该特定按钮的颜色,并且没有任何相关问题与此相关。我尝试使用“for”,但没有帮助:

package appli;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MainW extends JFrame implements ActionListener {

    public MainW(){
        setSize(640,480);
        setTitle("title");
        setLayout(null);
        JButton[][] btnz = new JButton[5][5];
        for(Integer i=0;i<5;i++)
        {
            for(Integer j=0;j<5;j++)
            {
                btnz[i][j]= new JButton("");
                btnz[i][j].setBackground(Color.WHITE);
                btnz[i][j].setBounds(10+20*i,10+20*j,20,20);
                add(btnz[i][j]);
                btnz[i][j].addActionListener(this);
            }
        }
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e){
        for(Integer i=0;i<5;i++)
        {
            for(Integer j=0;j<5;j++)
            {
                if (e.getSource()==btnz[i][j]);
                {
                    btnz[i][j].setBackground(Color.RED);
                }
            }
        }
    }

}

Let's say I have a program with 2D array of buttons, and when you click one of them it turns red. I didn't want to declare every single button seperately so I just created JButton[][] array for them. The problem is that I don't know how to use action listener on any of the buttons in the array so it would change the color of this particular button, and none of related questions is relevant to this. I tried to use "for" but it doesn't help:

package appli;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MainW extends JFrame implements ActionListener {

    public MainW(){
        setSize(640,480);
        setTitle("title");
        setLayout(null);
        JButton[][] btnz = new JButton[5][5];
        for(Integer i=0;i<5;i++)
        {
            for(Integer j=0;j<5;j++)
            {
                btnz[i][j]= new JButton("");
                btnz[i][j].setBackground(Color.WHITE);
                btnz[i][j].setBounds(10+20*i,10+20*j,20,20);
                add(btnz[i][j]);
                btnz[i][j].addActionListener(this);
            }
        }
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e){
        for(Integer i=0;i<5;i++)
        {
            for(Integer j=0;j<5;j++)
            {
                if (e.getSource()==btnz[i][j]);
                {
                    btnz[i][j].setBackground(Color.RED);
                }
            }
        }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里兽 2024-10-17 06:04:29

您尝试过吗:

public void actionPerformed(ActionEvent e){
   if( e.getSource() instanceof JButton) {
       ((JButton)e.getSource()).setBackground(Color.red);
   }
}

您的代码可能无法工作的一个原因是您用来创建按钮的 JButton[][] 是您的 MainW 构造函数的本地函数。我提供的方法将允许您忽略这个范围问题。它还将删除您的迭代方法,并用更有效的解决方案取而代之。在您的代码中,即使事件是由列表中的第一项触发的,或者甚至不是由其中一个按钮触发的,您也将始终迭代整个 2D 数组并测试每个数组。

have you tried:

public void actionPerformed(ActionEvent e){
   if( e.getSource() instanceof JButton) {
       ((JButton)e.getSource()).setBackground(Color.red);
   }
}

One reason why your code might not work is that the JButton[][] you use to create your buttons is local to your MainW constructor. The approach I have provided will allow you to disregard this scoping issue. It will also remove your iterative approach, and replace it with a more efficient solution. In your code, even if the event is triggered by the first item in your list, or even not triggered by one of your buttons, you will always iterate over the entire 2D array and test each.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文