使用Java方法getImage()创建的问题?

时间:2021-08-12 21:21:18

So I'm attempting to create a simple application in which I can click a button and allow the user to display an image of their choice using a jFIleChooser from the menu and display that image into a label. I wrote code that works for that, but I'm supposed to create and use a method called "BufferedImage getImage()" using a class "ImageManipulator" to actually run that code, so that I can call on that method rather than running all the code in the main class. Here is what I have: (My ImageManipulator class)

所以我试图创建一个简单的应用程序,我可以在其中单击一个按钮,允许用户使用菜单中的jFIleChooser显示他们选择的图像,并将该图像显示到标签中。我编写了适用于此的代码,但我应该创建并使用一个名为“BufferedImage getImage()”的方法,使用类“ImageManipulator”来实际运行该代码,这样我就可以调用该方法而不是全部运行主类中的代码。这是我所拥有的:(我的ImageManipulator类)

public class ImageManipulator extends MainWindow {
BufferedImage image;

public ImageManipulator() {
    image = null;
}

public ImageManipulator(BufferedImage image){
    this.image = image;
}

public BufferedImage getImage() {
    FileChooser.showOpenDialog(null);
    BufferedImage image = null;
       try{
           File myFile = FileChooser.getSelectedFile();

           image = ImageIO.read(myFile);

           labelImage.setIcon(new ImageIcon(image));
       }
       catch(IOException e){

    }
    return image;

}

and my main window for the GUI (the code that actually goes under buttonClicked):

和GUI的主窗口(实际上在buttonClicked下的代码):

private void buttonChooseActionPerformed(java.awt.event.ActionEvent evt) {                                             


        ImageManipulator myManipulator = new ImageManipulator(image);
        myManipulator.getImage();


}   

So it presents an error because the image variable within myManipulator has not been declared or initialized. I realize that I need to initialize the image variable above in my main window and "connect" it to the image variable that is being returned via getImage() in some way, I'm just not sure about how to do so or what to set image equal to so that it actually runs the code in the getImage() method. I'm new to using classes and OO programming so hopefully someone can give me some direction here.

因此它会出现错误,因为myManipulator中的图像变量尚未声明或初始化。我意识到我需要在我的主窗口中初始化上面的图像变量并将其“连接”到以某种方式通过getImage()返回的图像变量,我只是不确定如何这样做或者什么将图像设置为等于实际运行getImage()方法中的代码。我是新手使用类和OO编程,所以希望有人可以给我一些方向。

1 个解决方案

#1


0  

In order to get the method to work properly I deleted the second constructor

为了使方法正常工作,我删除了第二个构造函数

public ImageManipulator(BufferedImage image){ this.image = image; }

public ImageManipulator(BufferedImage image){this.image = image; }

and used the first constructor with some small changes made

并使用第一个构造函数进行了一些小的更改

public ImageManipulator() { this.image = image; }

public ImageManipulator(){this.image = image; }

when creating ImageManipulator myManipulator = new ImageManipulator();because there's actually no need to pass myManipulator an argument in this case.

在创建ImageManipulator时myManipulator = new ImageManipulator();因为在这种情况下实际上不需要传递myManipulator参数。

#1


0  

In order to get the method to work properly I deleted the second constructor

为了使方法正常工作,我删除了第二个构造函数

public ImageManipulator(BufferedImage image){ this.image = image; }

public ImageManipulator(BufferedImage image){this.image = image; }

and used the first constructor with some small changes made

并使用第一个构造函数进行了一些小的更改

public ImageManipulator() { this.image = image; }

public ImageManipulator(){this.image = image; }

when creating ImageManipulator myManipulator = new ImageManipulator();because there's actually no need to pass myManipulator an argument in this case.

在创建ImageManipulator时myManipulator = new ImageManipulator();因为在这种情况下实际上不需要传递myManipulator参数。