Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图像/图标使用相对路径_Java_Eclipse_Relative Path - Fatal编程技术网

Java 图像/图标使用相对路径

Java 图像/图标使用相对路径,java,eclipse,relative-path,Java,Eclipse,Relative Path,我真的不知道如何在Eclipse中正确使用相对路径。我在src文件夹旁边创建了一个res文件夹,其中有一个图像文件夹 这是我当前的代码: public class ToolbarView extends JToolBar { JButton addButton = new JButton("\\images\\button.png"); 试着这样做 JButton addButton = new JButton(new ImageIcon(getClass().getResource(

我真的不知道如何在Eclipse中正确使用相对路径。我在src文件夹旁边创建了一个res文件夹,其中有一个图像文件夹

这是我当前的代码:

public class ToolbarView extends JToolBar {
    JButton addButton = new JButton("\\images\\button.png");
试着这样做

JButton addButton = new JButton(new ImageIcon(getClass().getResource("images\button.png")));
试着这样做

JButton addButton = new JButton(new ImageIcon(getClass().getResource("images\button.png")));

您可以使用
getClass().getResource(…)
创建一个图标,如下所示:

 Icon icon = new ImageIcon(getClass().getResource("/images/button.png"));
 JButton b = new JButton(icon);

您可以使用
getClass().getResource(…)
创建一个图标,如下所示:

 Icon icon = new ImageIcon(getClass().getResource("/images/button.png"));
 JButton b = new JButton(icon);

你可以这样写

JButton addButton = new JButton(new ImageIcon("images/button.png"));

你可以这样写

JButton addButton = new JButton(new ImageIcon("images/button.png"));

你不必写下/res/image.png你只需要写/image.png。这对我很有用。(顺便说一句,图像在src/main/resources文件夹中)

你不需要写下/res/image.png你只需要写/image.png。这对我来说很有效。(顺便说一下,图像位于src/main/resources文件夹中)

remove\\infort
images
remove\\infort
images