简体   繁体   中英

I'm getting an error with calling a method from another class

I know this gets asked alot but I can't find a solution to my problem. I assume my arguments are wrong in some way.

public class ImageFileHandler extends FileHandler {


public void displayImage() {
    //Displaying an image
    JLabel picLabel = new JLabel(new ImageIcon(img));

    JPanel jPanel = new JPanel();
    jPanel.add(picLabel);

    JFrame frame = new JFrame();
    frame.setSize(new Dimension(img.getWidth(),
            img.getHeight()));
    frame.add(jPanel);
    frame.setVisible(true);
}

}

I want to call this displayImage() method in the following class.

public class FileApplicationTester {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    BufferedImage img;

    try {
        img = ImageIO.read(new File("D:\\Documents\\University\\Year_3\\Advanced Programming\\Week3\\supplementary materials-20190206\\images.jpeg"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    ImageFileHandler Display = new ImageFileHandler();
    ImageFileHandler.displayImage();

}

}

I'm getting an error with ImageFileHandler.displayImage(); Tells me that the displayImage method needs to be static, but I don't want it to be static to that class right?

你创建的 ImageFileHandler 实例被称为 Display 所以像这样使用它: Display.displayImage();

你好,你有没有尝试过

Display.displayImage();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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