我在代码

时间:2015-10-22 20:17:02

标签: java swing

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;
}

}

在我的showWindow方法中,我收到的错误是无法找到该对象。我在showMenu方法中定义了对象。我被卡住了,我不知道该怎么办。

我应该做的事情:

此例程会将图像加载到内存中,non-static需要 一个东西。它期望传递给它的图像文件名的名称,并返回带有图像的JLabel。它将假设一个Internet连接可用,它只能被称为 AFTER 已创建程序对象,它将返回一个类型JLabel变量,调用它如下:

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

此代码要求您执行:

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

注意:此方法使用参数和返回类型进行输入/输出。

1 个答案:

答案 0 :(得分:0)

theobject是showMenu中的局部变量。它只会在这种方法中可见。

这就是为什么你没有在showWindow中看到它。你可以把它变成你班上的一个领域。