使用字符串参数

时间:2015-09-01 00:55:49

标签: java image swing jlabel imageicon

当我使用为ImageIcon写入的文件名设置面板时,它可以正常工作:

public TitlePanel(){
    setOpaque(false);
    setLayout(new BoxLayout( this, BoxLayout.Y_AXIS ));

    ImageIcon image = new ImageIcon("images/q0.png");
    JLabel imageLabel = new JLabel( image);
    add(Box.createRigidArea(new Dimension(150,40)));
    add(imageLabel);
}

但是,当我向ImageIcon传递一个字符串时,它会停止工作,没有错误消息。图像不会出现,但会打印出正确的字符串路径:

public static String imageName = "\"images/q0.png\"";

public TitlePanel(){
    setOpaque(false);
    setLayout(new BoxLayout( this, BoxLayout.Y_AXIS ));

    ImageIcon image = new ImageIcon(imageName);
    System.out.println(imageName);
    JLabel imageLabel = new JLabel( image);
    add(Box.createRigidArea(new Dimension(150,40)));
    add(imageLabel);

}

文件层次结构如下:

  1. PROJECTNAME
    • SRC
      • TitlePanel
    • 图像
  2. 有谁知道为什么会导致ImageIcon无法找到该文件?

1 个答案:

答案 0 :(得分:2)

interface JQuery { 
    bootstrapToggle: any;
}

引号不应该是文件名的一部分。

代码应为:

import time
import matplotlib.pyplot as plt


    plt.ion()
    plt.show()

    for i in range(10):
        time.sleep(1)
        x = i ** 2
        plt.scatter(i, x)
        plt.draw()

这不仅适用于文件名,也适用于任何变量。您不要将引号包含在字符串中。