我是 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) {
}
}
这是我的 RESOURCESMANAGER.JAVA
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
}
}
所以基本上 Button 需要来自 ResourcesManager 的 pButton
实际上是 playButton.png,它位于资源包中,与 ResourcesManager.java 相同的文件夹
请帮助我解决我的目录问题。
编辑:抱歉忘记了,没有错误,只是出现了一个空白按钮,没有我的图像。
至于我的目录,它看起来像
src
mainClasses (package)
mainFrame.java
runClass.java
resources (package)
kingdomraiderlogo.png
playButton.png (image wanted at the momment.)
ResourcesManager.java
我会+1