簡體   English   中英

顯示圖像時,Java Hello World應用程序無法啟動

[英]Java Hello World App Does Nothing on Launch When Image Displayed

我有一個使用JFrame / JPanel創建的小型“ hello world”應用程序,它在我的IDE(Netbeans)內可以完美運行。 我只是通過使用Graphics.drawString()在屏幕上繪制一個字符串來測試它的,它在瀏覽器和內置的.Jar文件中均能完美工作。

我決定添加一個圖像只是為了對其進行測試(我是Java的新手),該應用程序繼續在IDE中運行,但是內置的.Jar文件無法啟動任何程序,也不會給出錯誤或任何提示。

這是2個類文件:

Boxy_Main.java:

package boxy;

import javax.swing.JFrame;
import java.awt.*;

public class Boxy_Main extends JFrame {

    public static Boxy_Main main;

    public Boxy_Main() {
        add(new Board());
        setTitle("Boxy");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(467, 700);
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(true);
    }
    public static void main(String[] args) {
        main = new Boxy_Main();
    }
}

Board.java:

package boxy;

import javax.swing.JPanel;
import java.awt.*;
import javax.swing.*;

public class Board extends JPanel {

    public Graphics2D g2d;
    public Graphics g;
    public Image hello_image;

    public Board() {

        this.set_properties();

    }

    public void set_properties() {

        this.hello_image = new ImageIcon(Board.class.getResource("../images/LKRZV.jpg")).getImage();


    }

    public void paint(Graphics g) {

        this.g = g;
        this.g2d = (Graphics2D) g;


        this.g2d.drawImage(this.hello_image, null, this);

        this.g.drawString("hello", 100, 80);
        this.g.drawString("world", 100, 94);


    }
}

如果我注釋掉

this.hello_image = new ImageIcon(Board.class.getResource("../images/LKRZV.jpg")).getImage();

this.g2d.drawImage(this.hello_image, null, this);

它繼續完美地工作。

我試圖自己解決這個問題,我無法通過Google或堆棧溢出找到任何東西。 這是一個很難編寫的搜索短語。

任何人都知道為什么要在NetBeans中運行它,而不是將其作為獨立的.Jar?

您的問題在這一行

this.hello_image = new ImageIcon(Board.class.getResource("../images/LKRZV.jpg")).getImage();

在NetBeans中運行時,“ ../ images.LKRZV.jpg”路徑正確,但在運行JAR時,路徑不正確。 確保您提供的路徑相對於jar文件的位置是正確的。 如果您將該路徑更改為圖像的絕對路徑,則程序應該可以正常運行。 將圖像與罐子包裝在一起可能會更好。

暫無
暫無

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

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