sách gpt4 ai đã đi

(Java) Java không tìm thấy tệp hình ảnh của tôi

In lại 作者:行者123 更新时间:2023-12-01 15:24:46 31 4
mua khóa gpt4 Nike

我是 Java 新手。

我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课

Mã số:

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!");
}
khác
{
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 相同的文件夹

请帮助我解决目录问题。

编辑:抱歉忘记了,没有错误,只是出现一个空白按钮,没有我的图像。

至于我的目录,它看起来像

nguồn

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

我会+1

1 Câu trả lời

这样做:

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"))));

biên tập:

在 Java 中,有两种访问文件的方法。一种方法是通过使用正斜杠的路径,通常在类 Lớp học của getResource() 方法中使用。第一个斜杠引用项目的 src 文件夹。

第二种方式是路径名,例如在Tài liệu类的构造函数中使用。在 Windows 上,这会使用双反斜杠,因为必须转义反斜杠。问题在于平台独立性,通过在路径名字符串中使用 File.Separator 可以轻松重新获得平台独立性。

关于java - (Java) Java 找不到我的图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10318894/

31 4 0
Bài viết được đề xuất: java - Tomcat进程阻止其他进程重新绑定(bind)到porton重启
Bài viết được đề xuất: javascript - Sucrase 只移动 JS 文件
Bài viết được đề xuất: javascript - 将图像动态添加到这个漂亮的框架中
Bài viết được đề xuất: java - 内存游戏java
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com