2012-04-25 16 views
0

私はJavaを初めて使用しています。(Java)Javaが画像ファイルを見つけることができません。

私のJavaプログラムは、 JButtonとして使用しようとしているイメージファイルを見つけることができません。 (現在、このコードは何もしません。なぜなら、私は単純に望ましい外観を得ているからです。 )。 これは私のメインクラス

コードです:

package mainClasses; 
/* 
* Frame Info and all that shit, 
* mainFrame is the actual frame itself 
* it will refer to MainC.java a lot Main class = Main Class 
*/ 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.io.File; 

import resources.ResourcesManager; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 


@SuppressWarnings({ "unused", "serial" }) 
public class mainFrame extends JFrame { 

ResourcesManager rManager = new ResourcesManager(); 


public mainFrame() { 
    JButton playButton = new JButton(rManager.pButton); 
    JButton infoButton = new JButton(); 
    JButton exitButton = new JButton(); 


    int x = 310, y = 300; 
    setSize(x, y); 
    setVisible(true); 
    setLayout(null); 
    setTitle("Kingdom Raider"); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 

    /*Buttons and Properties*/ 
         /*X, Y, X, Y*/ 
    playButton.setBounds(10, 10, 70, 40); 
    /* Add if problem cannot be sorted */ // playButton.setText("Play"); 



    add(playButton); 

    infoButton.setBounds(90, 10, 110, 40); 
    infoButton.setText("Information"); 
    add(infoButton); 

    exitButton.setBounds(210, 10, 70, 40); 
    exitButton.setText("Exit"); 
    add(exitButton); 

    //This is for checking if the file is here. 
    File imageCheck = new File("/JavaGame/src/resources/playButton.png"); 

    if(imageCheck.exists()) 
    { 
     System.out.println("File found!"); 
    } 
    else 
    { 
     System.out.println("File not found!"); 
    } 

    repaint(); 



} 









public void Painting (Graphics g) { 


} 
} 

これは

package resources; 

import java.awt.Image; 

import javax.swing.ImageIcon; 
import javax.swing.JLabel; 

public class ResourcesManager { 

/*Here, your going to want to declare anything 
* needed. 
*/ 

public ImageIcon KRLogo = new ImageIcon("/JavaGame/src/resources/kingdomraiderlogo.png"); 
public ImageIcon pButton = new ImageIcon("/JavaGame/src/resources/playButton.png"); 

public void settings() { //Set the stuff settings, locations e.t.c. 
    //BLAH 

} 

} 

MY RESOURCESMANAGER.JAVAされるように、基本的にボタンがpButtonを必要とするResourcesManagerから

実際にはplayButton.pngはlocaですリソースパッケージ内のファイル は、ResourcesManager.javaと同じフォルダになります。

私のディレクトリの問題で私を助けてください。

編集:申し訳ありません、忘れてはいけません。画像がなくても、空白のボタンが表示されます。私はあなたが絶対(/リード)として、パスを1

+0

エラーメッセージは何ですか? –

+1

Javaはどこを探していますか?これを調べるには、次の結果を確認してください: 'System.out.println(System.getProperty(" user.dir "));' –

+0

ロードストリームまたはファイルに対して 'ClassLoader'を使うことを推奨します。 –

答えて

1

はこのようにそれを実行してください。 1つの方法は、スラッシュを使用するパス経由であり、これは通常ClassgetResource()メソッドで使用されます。最初のスラッシュは、プロジェクトのsrcフォルダを参照します。

第2の方法はpathnameであり、たとえばクラスFileのコンストラクタで使用されます。ウィンドウでは、バックスラッシュをエスケープする必要があるため、これは二重バックスラッシュを使用します。問題はプラットフォームの独立性であり、pathname文字列にFile.Separatorを使用することで簡単に取り戻せます。

0

定義しているよ

src 

mainClasses (package) 
    mainFrame.java 
    runClass.java 
resources (package) 
    kingdomraiderlogo.png 
    playButton.png (image wanted at the momment.) 
    ResourcesManager.java 

よう

は私のディレクトリについては、それが見えます。それは相対パスであるはずですか、またはあなたは本当にルートドライブのJavaGame/src/resources/...フォルダを持っていますか?

public ImageIcon KRLogo = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
(ResourcesManager.class.getResource("/resources/kingdomraiderlogo.png")))); 

public ImageIcon pButton = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
(ResourcesManager.class.getResource("/resources/playButton.png")))); 

編集:Javaでは

あなたがファイルにアクセスする2つの方法があり

+0

私は知っていません、私はちょうどEclipseの中で修飾名を使用し、括弧に貼り付けました。 –

関連する問題