简体   繁体   English

从ActionListener方法访问类变量

[英]Accessing class variables from ActionListener method

I am working with GUIs in Java using JFrame and JPanel, as well as ActionListener to edit an image when I click a button. 当我单击按钮时,我正在使用JFrame和JPanel使用Java中的GUI,以及ActionListener来编辑图像。 Currently, I am having a lot of trouble getting my JPanel class called ButtonPanel to interact with my BufferedImage img. 目前,让名为ButtonPanel的JPanel类与BufferedImage img交互时遇到很多麻烦。 I am trying to display the height of the image but nothing is working and I have tried various solutions. 我正在尝试显示图像的高度,但是没有任何效果,并且尝试了各种解决方案。 My code for the ButtonPanel class is: 我的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);
      }
   }

Whenever I try to use a method from the BufferedImage API in order to edit the image, such as in the above code, I get this error: 每当我尝试使用BufferedImage API中的方法来编辑图像时,例如在上面的代码中,都会出现此错误:

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

I have played around with where I initialize height but nothing has worked. 我曾在初始化高度的地方玩过,但没有任何效果。 Any help would be appreciated. 任何帮助,将不胜感激。

I have another class called ProgramWindow in which I add all the different JPanels of the image editor to one main JFrame and I think this is where my problem might be, as the BufferedImage is null. 我有另一个名为ProgramWindow的类,其中将图像编辑器的所有不同JPanels添加到一个主JFrame中,我认为这可能是我的问题所在,因为BufferedImage为null。 Here is the code for ProgramWindow: 这是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);
      }
   }

I have concluded that the ButtonPanel in ProgramWindow is being passed a null parameter but I do not know why this is. 我已经得出结论,正在向ProgramWindow中的ButtonPanel传递一个null参数,但是我不知道为什么这样做。 I have a method called getImg in the ImagePanel class which I am calling as the parameter for ButtonPanel. 我在ImagePanel类中有一个称为getImg的方法,该方法称为ButtonPanel的参数。 Here is the code for ImagePanel: 这是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;
        }
   }

You're declaring height in the constructor and so it is local to the constructor. 您在构造函数中声明了高度,因此它是构造函数的局部变量。 Perhaps it would be better to make it an instance field of the class instead. 也许最好将它设置为类的实例字段。

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

Having said that, why even have a height variable field, since you can obtain it any time you'd like by calling img.getHeight() ? 话虽如此,为什么还要有一个height变量字段,因为您可以通过调用img.getHeight()随时获取它?


Edit 编辑
Your second problem, one we can't help you on yet, is that your img variable holds a null reference. 您的第二个问题(我们尚无法解决)是img变量包含空引用。 Given your new code, this suggests that ip.getImg() is returning null , but don't take my word on this, test it. 给定您的新代码,这表明ip.getImg()返回null ,但是请不要相信我的话,对其进行测试。 Put this line in your code: 将此行放在您的代码中:

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

Edit 2 编辑2
Of course you're getting null. 当然,您会得到null。 You're extracting img from ImagePanel before the user has had a chance to interact with it. 您是在用户有机会与ImagePanel进行交互之前从ImagePanel中提取img的。 Since it only gets an image when the user presses the button, and since you're extracting the image on class creation, before the user has had a chance to do squat, the only possibility is that when you extract the image on class creation, you're going to get null. 由于只有在用户按下按钮时它才会获得图像,并且由于您是在创建类时提取图像的,因此在用户下蹲之前,唯一的可能性是,当您在类创建时提取图像时,您将得到null。 Solution: don't do that. 解决方案:不要那样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM