Domanda

I`m developing simple java application with visualization. I have to package it to jar with maven. My project structure is:
src
---main
--------java
-----------com.chess
--------resources
-----------Images

I tried to use images like : Images\image.jpg but its missing. How i can use images?

This is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ChessApp</groupId>
<artifactId>ChessApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.chess.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

È stato utile?

Soluzione

By your comment,

layout.setIcon(new ImageIcon("Images\\BlackRockAndBlackBoard.jpg"));

ImageIcon treats the string as a filename, you need to take a gander at Get a resource using getResource()

Then use the url for constructing the ImageIcon.

URL imageUrl = YourClassName.class.getResource("/Images/ImageName.jpg");
layout.setIcon(new ImageIcon( imageUrl ));

The URL is allowed as a constructor argument as you can see here http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html#ImageIcon%28java.net.URL%29.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top