Java 图像/图标使用相对路径
我真的不知道如何在Eclipse中正确使用相对路径。我在src文件夹旁边创建了一个res文件夹,其中有一个图像文件夹 这是我当前的代码: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(
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\\infortimages