【问题标题】:Java ActionListener Error , Buttons Color Changing Program using popupsJava ActionListener 错误,使用弹出窗口的按钮颜色更改程序
【发布时间】:2015-02-23 08:44:25
【问题描述】:

我正在尝试制作一个程序,将框架中的不同颜色更改为我使用弹出菜单选择的颜色。我在 ActionListener 中遇到错误,如果没有它,我将无法为每个彩色按钮执行操作。

Exception in thread "main" java.lang.NullPointerException
    at Graphic.<init>(Graphic.java:43)
    at Graphic.main(Graphic.java:273) 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*; 
public class Graphic extends JFrame implements ActionListener
{
    JFrame frame = new JFrame("Graphic");
    private JButton [] l;
        private JPopupMenu menu;
        private Toolkit toolkit;
public void actionPerformed(ActionEvent actionEvent) {
    System.out.println("Selected: " + actionEvent.getActionCommand());
  }
    public Graphic ()
    {
        setLayout(new GridLayout(3,3));
                JButton l[]=new JButton[9];
                for ( int i=0;i<9;i++ )
                {
        l[i] = new JButton();
        add(l[i]);
                if(i==0)
        l[i].setBackground(Color.GREEN);
                else if(i==1)
        l[i].setBackground(Color.BLUE);
                else if(i==2)
        l[i].setBackground(Color.RED);
                else if(i==3)
        l[i].setBackground(Color.WHITE);
                else if(i==4)
        l[i].setBackground(Color.PINK);
                else if(i==5)
        l[i].setBackground(Color.GRAY);
                else if(i==6)
        l[i].setBackground(Color.CYAN);
                else if(i==7)
        l[i].setBackground(Color.MAGENTA);
                else if(i==8)
        l[i].setBackground(Color.BLACK);
                for(JButton j:l)
                {
                    j.addActionListener(this);
                }
                }

        //Create the popup menu.
         //private class ColorListener implements ActionListener{
        final JPopupMenu popup = new JPopupMenu();

        popup.add(new JMenuItem(new AbstractAction("Green") {

            public void actionPerformed(ActionEvent e) {
               if(e.getSource() == l[0])
                   l[0].setBackground(Color.GREEN);
               else if(e.getSource() == l[1])
                   l[1].setBackground(Color.GREEN);
               else if(e.getSource() == l[2])
                   l[2].setBackground(Color.GREEN);
               else if(e.getSource() == l[3])
                   l[3].setBackground(Color.GREEN);
               else if(e.getSource() == l[4])
                   l[4].setBackground(Color.GREEN);
               else if(e.getSource() == l[5])
                   l[5].setBackground(Color.GREEN);
               else if(e.getSource() == l[6])
                   l[6].setBackground(Color.GREEN);
               else if(e.getSource() == l[7])
                   l[7].setBackground(Color.GREEN);
               else if(e.getSource() == l[8])
                   l[8].setBackground(Color.GREEN);

        }

       }));

        popup.add(new JMenuItem(new AbstractAction("Blue") {
            public void actionPerformed(ActionEvent e) {
               if(e.getSource() == l[0])
                   l[0].setBackground(Color.BLUE);
               else if(e.getSource() == l[1])
                   l[1].setBackground(Color.BLUE);
               else if(e.getSource() == l[2])
                   l[2].setBackground(Color.BLUE);
               else if(e.getSource() == l[3])
                   l[3].setBackground(Color.BLUE);
               else if(e.getSource() == l[4])
                   l[4].setBackground(Color.BLUE);
               else if(e.getSource() == l[5])
                   l[5].setBackground(Color.BLUE);
               else if(e.getSource() == l[6])
                   l[6].setBackground(Color.BLUE);
               else if(e.getSource() == l[7])
                   l[7].setBackground(Color.BLUE);
               else if(e.getSource() == l[8])
                   l[8].setBackground(Color.BLUE);

            }

        }));
         popup.add(new JMenuItem(new AbstractAction("Red") {
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == l[0])
                   l[0].setBackground(Color.RED);
               else if(e.getSource() == l[1])
                   l[1].setBackground(Color.RED);
               else if(e.getSource() == l[2])
                   l[2].setBackground(Color.RED);
               else if(e.getSource() == l[3])
                   l[3].setBackground(Color.RED);
               else if(e.getSource() == l[4])
                   l[4].setBackground(Color.RED);
               else if(e.getSource() == l[5])
                   l[5].setBackground(Color.RED);
               else if(e.getSource() == l[6])
                   l[6].setBackground(Color.RED);
               else if(e.getSource() == l[7])
                   l[7].setBackground(Color.RED);
               else if(e.getSource() == l[8])
                   l[8].setBackground(Color.RED);

            }
        }));
         popup.add(new JMenuItem(new AbstractAction("Pink") {
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == l[0])
                   l[0].setBackground(Color.PINK);
               else if(e.getSource() == l[1])
                   l[1].setBackground(Color.PINK);
               else if(e.getSource() == l[2])
                   l[2].setBackground(Color.PINK);
               else if(e.getSource() == l[3])
                   l[3].setBackground(Color.PINK);
               else if(e.getSource() == l[4])
                   l[4].setBackground(Color.PINK);
               else if(e.getSource() == l[5])
                   l[5].setBackground(Color.PINK);
               else if(e.getSource() == l[6])
                   l[6].setBackground(Color.PINK);
               else if(e.getSource() == l[7])
                   l[7].setBackground(Color.PINK);
               else if(e.getSource() == l[8])
                   l[8].setBackground(Color.PINK);

            }
        }));
         popup.add(new JMenuItem(new AbstractAction("Gray") {
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == l[0])
                   l[0].setBackground(Color.GRAY);
               else if(e.getSource() == l[1])
                   l[1].setBackground(Color.GRAY);
               else if(e.getSource() == l[2])
                   l[2].setBackground(Color.GRAY);
               else if(e.getSource() == l[3])
                   l[3].setBackground(Color.GRAY);
               else if(e.getSource() == l[4])
                   l[4].setBackground(Color.GRAY);
               else if(e.getSource() == l[5])
                   l[5].setBackground(Color.GRAY);
               else if(e.getSource() == l[6])
                   l[6].setBackground(Color.GRAY);
               else if(e.getSource() == l[7])
                   l[7].setBackground(Color.GRAY);
               else if(e.getSource() == l[8])
                   l[8].setBackground(Color.GRAY);

            }
        }));
         popup.add(new JMenuItem(new AbstractAction("Yellow") {
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == l[0])
                   l[0].setBackground(Color.YELLOW);
               else if(e.getSource() == l[1])
                   l[1].setBackground(Color.YELLOW);
               else if(e.getSource() == l[2])
                   l[2].setBackground(Color.YELLOW);
               else if(e.getSource() == l[3])
                   l[3].setBackground(Color.YELLOW);
               else if(e.getSource() == l[4])
                   l[4].setBackground(Color.YELLOW);
               else if(e.getSource() == l[5])
                   l[5].setBackground(Color.YELLOW);
               else if(e.getSource() == l[6])
                   l[6].setBackground(Color.YELLOW);
               else if(e.getSource() == l[7])
                   l[7].setBackground(Color.YELLOW);
               else if(e.getSource() == l[8])
                   l[8].setBackground(Color.YELLOW);

            }
        }));
         popup.add(new JMenuItem(new AbstractAction("Cyan") {
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == l[0])
                   l[0].setBackground(Color.CYAN);
               else if(e.getSource() == l[1])
                   l[1].setBackground(Color.CYAN);
               else if(e.getSource() == l[2])
                   l[2].setBackground(Color.CYAN);
               else if(e.getSource() == l[3])
                   l[3].setBackground(Color.CYAN);
               else if(e.getSource() == l[4])
                   l[4].setBackground(Color.CYAN);
               else if(e.getSource() == l[5])
                   l[5].setBackground(Color.CYAN);
               else if(e.getSource() == l[6])
                   l[6].setBackground(Color.CYAN);
               else if(e.getSource() == l[7])
                   l[7].setBackground(Color.CYAN);
               else if(e.getSource() == l[8])
                   l[8].setBackground(Color.CYAN);

            }
        }));
         popup.add(new JMenuItem(new AbstractAction("Magenta") {
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == l[0])
                   l[0].setBackground(Color.MAGENTA);
               else if(e.getSource() == l[1])
                   l[1].setBackground(Color.MAGENTA);
               else if(e.getSource() == l[2])
                   l[2].setBackground(Color.MAGENTA);
               else if(e.getSource() == l[3])
                   l[3].setBackground(Color.MAGENTA);
               else if(e.getSource() == l[4])
                   l[4].setBackground(Color.MAGENTA);
               else if(e.getSource() == l[5])
                   l[5].setBackground(Color.MAGENTA);
               else if(e.getSource() == l[6])
                   l[6].setBackground(Color.MAGENTA);
               else if(e.getSource() == l[7])
                   l[7].setBackground(Color.MAGENTA);
               else if(e.getSource() == l[8])
                   l[8].setBackground(Color.MAGENTA);

            }
        }));
         popup.add(new JMenuItem(new AbstractAction("Black") {
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == l[0])
                   l[0].setBackground(Color.BLACK);
               else if(e.getSource() == l[1])
                   l[1].setBackground(Color.BLACK);
               else if(e.getSource() == l[2])
                   l[2].setBackground(Color.BLACK);
               else if(e.getSource() == l[3])
                   l[3].setBackground(Color.BLACK);
               else if(e.getSource() == l[4])
                   l[4].setBackground(Color.BLACK);
               else if(e.getSource() == l[5])
                   l[5].setBackground(Color.BLACK);
               else if(e.getSource() == l[6])
                   l[6].setBackground(Color.BLACK);
               else if(e.getSource() == l[7])
                   l[7].setBackground(Color.BLACK);
               else if(e.getSource() == l[8])
                   l[8].setBackground(Color.BLACK);

            }
        }));
        for(int j=0;j<9;j++)                
        l[j].addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        });

        }

    public static void main (String args[])
    {
        Graphic g = new Graphic();
        g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        g.setSize(300, 300);
        g.setVisible(true);
    }

}

【问题讨论】:

  • 不要只是转储代码并期望有人修复它
  • 我不是要修复,我只是想知道如何修复它:)
  • 添加 ActionListener 的内部循环看起来令人担忧,您不能尝试将 ActionListener 添加到尚未初始化的按钮中
  • 首先发布的代码甚至无法编译,因为您定义了两次按钮数组。此外,发布的代码格式不正确,导致代码难以阅读。如果您希望人们阅读您的代码,请让代码可读。
  • 很抱歉,但我还在学习 Java 不是专业人士,这就是为什么

标签: java swing actionlistener helpers


【解决方案1】:

看看这个部分...

            for ( int i=0;i<9;i++ )
            {
                l[i] = new JButton();
                //...
                for(JButton j:l)
                {
                    j.addActionListener(this);
                }
            }

基本上,您的内部循环正在尝试访问数组中尚未初始化的元素...

当你有机会时,在你的代码中放一个断点并单步执行它,这会给你一些重要的经验

【讨论】:

  • 1+,当我被电话打断时,我几乎输入了我的答案:) 真正的问题是为什么他甚至有一个内部循环。只需在创建按钮时将 ActionListener 添加到按钮。发布代码的另一个问题是按钮数组的shadowing
  • 我会看看如何处理这部分,谢谢你们,但是弹出菜单的 ActionListener 呢?有什么想法吗?
  • @PrinceQuestiorinos 您的弹出菜单项 ActionListener 不会像您想象的那样工作,因为事件的来源实际上是菜单项本身。看看 setComponentPopupMenu
猜你喜欢
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
相关资源
最近更新 更多