2

Eclipseでは、コードを実行すると、次のように機能します。

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


   public class Main {

    public static void main(String[] args) {

        JFrame frame = new JFrame("test viewing images");

        frame.setSize(600,300);     
        frame.setLocationRelativeTo(null); // centered on monitor   
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        /**
         * Menu Bar stuff
         */

        JMenuBar menuBar;
        JMenu menu;
        JMenuItem menuItem;

        // MENU BAR 
        menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        menuBar.setVisible(true);

        // MENU 1
        menu = new JMenu("File");
        menuBar.add(menu);

            // MENU 1 ITEM
            ImageIcon icon = new ImageIcon("src/Action-exit-icon.png");     
            menuItem = new JMenuItem("Exit Program", icon);
            menu.add(menuItem);


        frame.setVisible(true);

    }

   }

そして、これが私のパッケージエクスプローラーからのファイル構造です:

ShowImage (project)
 > src / Main.java
 > src / Action-exit-icon.png

また、このワークスペースはZ:\eclipse_projectsにあります

ImageIcon icon = new ImageIcon( "src / Action-exit-icon.png");が表示されます。はうまく機能しており、menuBarがその役割を果たします。

それでは、このプロジェクトをエクスポートしましょう。JARを私の友人にメールで送信します。

  1. プロジェクトを右クリック>[エクスポート]を選択
  2. 「Java」>「実行可能なJARファイル」を選択します
  3. 起動構成でメインファイルを選択します
  4. エクスポート先:デスクトップ
  5. ライブラリ処理:必要なライブラリを生成されたJARに抽出します
  6. デスクトップに移動し、ShowImage.jarをダブルクリックします

JFrameは表示されますが、Action-exit-icon.pngはまったく表示されません。

ShowImage.jarを開くと、その内容を表示するために、Main.class、Action-exit-icon.png、META-INFが表示されます。

わかりました。画像やその他のリソースを参照する方法について、今は真剣に混乱しています。私は何が間違っているのですか?

4

1 に答える 1

7
new ImageIcon("src/Action-exit-icon.png"); 

StringコンストラクターImageIconは、文字列がパスを表すことを前提としていFileます。

このイメージは明らかにアプリケーションリソースであり、デプロイ時(Jar内)までに埋め込みリソースになります。URLしたがって、次のように、アプリの実行時クラスパスからアクセスする必要があります。

new ImageIcon(getClass().getResource("/src/Action-exit-icon.png")); 

コードをオーバーホールすると、次のようになります。

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

public class JavaGui148 {

    public JComponent getGUI() {
        JPanel p = new JPanel();

        p.setBackground(Color.GREEN);

        return p;
    }

    public JMenuBar getMenuBar() {
        /**
         * Menu Bar stuff
         */
        JMenuBar menuBar;
        JMenu menu;
        JMenuItem menuItem;

        // MENU BAR 
        menuBar = new JMenuBar();
        menuBar.setVisible(true);

        // MENU 1
        menu = new JMenu("File");
        menuBar.add(menu);

        // MENU 1 ITEM
        ImageIcon icon = new ImageIcon(getClass().getResource(
                "/src/Action-exit-icon.png"));
        menuItem = new JMenuItem("Exit Program", icon);
        menu.add(menuItem);

        return menuBar;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JavaGui148 gui = new JavaGui148();

                JFrame f = new JFrame("Demo");
                f.setJMenuBar(gui.getMenuBar());
                f.add(gui.getGUI());
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}
于 2012-12-19T00:50:38.573 に答える