簡體   English   中英

無法將Java eclipse項目中的圖像導出到可運行的jar

[英]Trouble exporting to runnable jar in java eclipse project with images

我在將第一個項目導出到可執行jar文件時遇到麻煩。 它在eclipse中運行得很好,但是在導出后雙擊可執行jar時,它什么也沒做。 我創建了多個測試程序並搜索了互聯網以尋求幫助,並得出結論認為,這與程序中的圖像有關。 我已經包含了測試程序中的所有代碼。 它不會讓我在項目中發布圖像文件位置的圖片,但是它位於源文件夾中。

import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class test {

    private JFrame frame;
    JLabel label;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    test window = new test();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public test() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        label = new JLabel("");
        Image img = new   ImageIcon(this.getClass().getResource("/YahtzeeBoard.png")).getImage();
        label.setIcon(new ImageIcon(img));
        label.setBounds(100, 100, 450, 300);
        frame.getContentPane().add(label);
    }

}

例如,制作一個像Icons之類的Java程序包,將圖像放在其中,然后調用

 getResource("Icons/YahtzeeBoard.png")

這樣,圖像將被包裝在罐子中。

或者您可以將圖像與jar放在相同的文件夾中,以找到路徑“ /YahtzeeBoard.png”,因為它指向jar所在的文件夾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM