简体   繁体   中英

I'm receiving a cannot find symbol error in my code

import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;

public class PictureViewer {

final static int MIN_NUMBER = 1;
final static int MAX_NUMBER = 8;
static int image_number = 1;
static String filename;

public static void main(String[] args) {

    showMenu();

}

public static int forward(int current_number) {
    if (current_number < MAX_NUMBER) {
        current_number++;
    } else {
        current_number = MAX_NUMBER;
    }
    return current_number;
}

public static int backward(int current_number) {
    if (current_number > MIN_NUMBER) {
        current_number--;
    }
    return current_number;
}

public static String createFileName(int current_number) {
    return ("Picture " + current_number + ".jpg");

}

public static String createRandomName() {
    return ("Picture " + (int) (Math.random() * 8 + 1) + ".jpg");
}

public static void showMenu() {

    PictureViewer theobject = new PictureViewer();

    int current_number = 0;

    Scanner input = new Scanner(System.in);
    while (true) {
        System.out.println("Choose static forward(1), static backward(2), createFileName(3), createRandomName(4)");
        int user = input.nextInt();

        switch (user) {
            case 1:
                System.out.println("static forward");
                current_number = forward(current_number);
                theobject.forward();
                break;
            case 2:
                System.out.println("static backward");
                current_number = backward(current_number);
                theobject.backward();
                break;
            case 3:
                System.out.println("createFileName");
                filename = createFileName(current_number);
                theobject.showWindow(createFileName(current_number));
                break;
            case 4:
                System.out.println("createRandomName");
                filename = createRandomName();
                theobject.showWindow(createRandomName());

        }
        if (image_number != 1);
        System.out.println(image_number);
    }

}

public void forward() {

    if (image_number < MAX_NUMBER) {
        image_number++;
    } else {
        image_number = MAX_NUMBER;
    }
}

public void backward() {

    if (image_number > MIN_NUMBER) {
        image_number--;
    }
}

public void showWindow(String filename) {

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JLabel jlblName = new JLabel();
    jlblName = theobject.load_picture(filename);

    panel.add(jlblName);
    frame.setTitle(filename);
    frame.setSize(450, 100);
    frame.setLocation(200, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}

public JLabel load_picture(String imagefile) {
    JLabel templabel = null;
    String startURL = "";
    if (!imagefile.startsWith("http")) {
        startURL = "http://riveira.x10host.com/images/";
    }
    URL myURL = null;
    try {
        myURL = new URL(startURL + imagefile);
        BufferedImage myPicture = ImageIO.read(myURL);
        templabel = new JLabel(new ImageIcon(myPicture));
    } catch (Exception e) {
        System.out.println("Error caught " + e.toString());
    }
    return templabel;
}

}

In my showWindow method, I'm receiving an error that says that the object cannot be found . I've defined the object within my showMenu method. I'm stuck and I'm not sure what to do.

What I'm supposed to do:

This routine will load an image into memory, non-static requires an object. It expects the name of the image file name passed to it and returns a JLabel with the image. It will assume an Internet conection is available and it can only be called AFTER the program object has been created, it will return a type JLabel variable, call it like this:

thelabel = object.load_picture("picture1.jpg");
(hard code 'picture1.jpg' only when testing - USE a method or // variable for 'real' call).

This code requires you to do an:

import java.awt.*
import java.net.*

Note: this method is using parameter and return type for input/output.

theobject is a local variable in showMenu. It will just be visible in this method.

That is why you do not see it in showWindow. You could make it a field in your class.

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