首页 文章

从ActionListener方法访问类变量

提问于
浏览
1

我使用JFrame和JPanel在Java中使用GUI,以及在单击按钮时使用ActionListener编辑图像 . 目前,我在使用名为ButtonPanel的JPanel类与我的BufferedImage img进行交互时遇到了很多麻烦 . 我试图显示图像的高度,但没有任何工作,我尝试了各种解决方案 . 我的ButtonPanel类的代码是:

class ButtonPanel extends JPanel
   {
        //JButton makeBlue;
      BufferedImage img;
       ButtonPanel(BufferedImage x)
      {
            final JButton makeBlue = new JButton ("Make Blue");
            img = x;
            //int width, height;
         setBackground(Color.RED);
         int height = 0;
         add(makeBlue);
         ActionListener action = 
             new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
               {


                  if (e.getSource()== makeBlue)
                  {
                            //img = x;
                         height = img.getHeight();
//                       System.out.println(rgbvalue);

                             //System.out.println(width + " " + height);
                      setBackground(Color.GREEN);
                     repaint();

                  }
               }

            };

         makeBlue.addActionListener(action);
      }
   }

每当我尝试使用BufferedImage API中的方法来编辑图像时,例如在上面的代码中,我都会收到此错误:

ImageEditorDeluxe.java:184: error: local variable height is accessed from within inner class; needs to be declared final
                         height = img.getHeight();

我已经玩过初始化高度的地方,但没有任何效果 . 任何帮助,将不胜感激 .

我有另一个名为ProgramWindow的类,我在其中将图像编辑器的所有不同JPanel添加到一个主JFrame中,我认为这是我的问题所在,因为BufferedImage为null . 这是ProgramWindow的代码:

class ProgramWindow extends JFrame  
   {
       ProgramWindow()
      {
         ImagePanel ip = new ImagePanel();
         ChooseFile cf = new ChooseFile();
         ButtonPanel bp = new ButtonPanel(ip.getImg());

         add(ip, BorderLayout.CENTER);
         add(cf, BorderLayout.SOUTH);
         add(bp, BorderLayout.WEST);
      }
   }

我得出结论,ProgramWindow中的ButtonPanel正在传递一个null参数,但我不知道为什么会这样 . 我在ImagePanel类中有一个名为getImg的方法,我将其作为ButtonPanel的参数调用 . 这是ImagePanel的代码:

class ImagePanel extends JPanel
   {
      BufferedImage img;

       ImagePanel()
      {
         setBackground(Color.BLUE);  //to test
         final JButton button = new JButton ("Display picture");
         add(button);       

         ActionListener action = 
             new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
               {
                  if (e.getSource()==button)
                  {
                     try
                     {
                        img = ImageIO.read(ChooseFile.getFile());
                     }
                         catch(IOException f)
                        {
                           f.printStackTrace();
                        }

                     repaint();

                  }
               }

            };

         button.addActionListener(action);
      }

       public void paintComponent(Graphics g)
      {
         super.paintComponent(g);
         if (img != null)
            g.drawImage(img, 0, 0, this);
      }

       public void setImage(BufferedImage i)
      {
         img = i;
         repaint();
      }
        public BufferedImage getImg()
        {
            return img;
        }
   }

1 回答

  • 2

    您在构造函数中声明高度,因此它是构造函数的本地高度 . 也许最好将它变成类的实例字段 .

    public class ButtonPanel extends JPanel {
        private BufferedImage img;
        private int height;
    

    话虽如此,为什么甚至有一个高度变量字段,因为您可以通过调用 img.getHeight() 随时获取它?


    Edit
    你的第二个问题,我们无法帮助你,是你的img变量持有一个空引用 . 鉴于你的新代码,这表明 ip.getImg() 正在返回 null ,但是不要接受我的话,测试它 . 把这一行放在你的代码中:

    System.out.println("is ip.getImg() returning null? " + (ip.getImg()));
    

    Edit 2
    当然,在用户有机会与之交互之前,你会从ImagePanel中重新提取img . 因为它仅在用户按下按钮时获取图像,并且因为您将获得null . 解决方案:不要那样做 .

相关问题