使用 BufferdImage 和 ImageIO 加载图像时,它会清除我的整个帧并且不显示图像

When Loading Image Using BufferdImage and ImageIO it Clears my Whole Frame and Does Not Display Image

我并使用涉及按钮、框架和面板的基本 GUI 制作了一个程序,在我尝试从我的项目文件夹中加载图像之前,一切都很好。当我添加代码行时

   try{
         titleImage = ImageIO.read(new File("mouse_title_resize.png"));
       } 
    catch(Exception e){}

在我 运行 程序之后,我的整个框架都变成了空白,而在我在 it.All 上有一些 JButtons 之前,我在 try-catch 行之前的代码工作得很好,我测试了看唯一破坏它的是这行代码。我没有收到任何错误或任何错误,我的项目文件夹中有图像,看起来图像加载正常,除了它不会显示在框架上,框架上的其他所有内容都消失了。我只是不明白为什么当我加载图像时它会清除我的整个画面。

完整代码如下:

这是扩展 JFrame

的 class

包 mouse.click.game;

进口java.awt.Dimension;

进口java.awt.Point;

进口java.awt.Toolkit;

进口javax.swing.JFrame;

public class MouseClickGame 扩展了 JFrame {

//Constants to define the frame width and height including borders
public final int FRAME_WIDTH = 600;
public final int FRAME_HEIGHT = 600;
//Dimension from Toolkit to be able to get width and height of screen
public Dimension sizeTool = Toolkit.getDefaultToolkit().getScreenSize();
//Using sizeTool to get width of screen
public double xResolution = sizeTool.getWidth();
//Using sizeTool to get height of screen
public double yResolution = sizeTool.getHeight();
//Creating a point object that is defined as the center of the screen
public Point middleOfScreen = new Point((int) (xResolution / 2) - (FRAME_WIDTH / 2), (int) (yResolution / 2) - (FRAME_HEIGHT / 2));

public MouseClickGame() {
    super("WELCOME :D");
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setLocation(middleOfScreen);
    add(new MouseClickPanel());

}

public static void main(String[] args) {

    //Calling constructor
    MouseClickGame mainClickGame = new MouseClickGame();

}

}

这里是扩展 JPanel 的 class(这是我项目中仅有的两个 class)

包 mouse.click.game;

进口java.awt.Dimension;

进口java.awt.image.BufferedImage;

进口java.io.File;

进口java.io.IOException;

进口javax.imageio.ImageIO;

进口javax.swing.Box;

导入javax.swing.Box布局;

进口javax.swing.ImageIcon;

进口javax.swing.JButton;

进口javax.swing.JLabel;

进口javax.swing.JPanel;

public class MouseClickPanel 扩展 JPanel {

JButton buttonPlay = new JButton("Play");
JButton buttonContinue = new JButton("Continue");
JButton buttonOptions = new JButton("Options");
JButton buttonExit = new JButton("Exit");

BoxLayout boxLay = new BoxLayout(this, BoxLayout.Y_AXIS);

Dimension menuButtonSize = new Dimension(300, 30);
Dimension spacingBetweenButtons = new Dimension(0, 30);

BufferedImage titleImage;

public MouseClickPanel() {

    try {
        titleImage = ImageIO.read(new File("C:\Users\Justin\Desktop\mouse_title.png"));
    } catch (IOException e) {

    }

    setLayout(boxLay);
    add(Box.createVerticalGlue());
    add(new JLabel(new ImageIcon(titleImage)));

    //Adding glue to force buttons away from top of panel
    add(Box.createVerticalGlue());
    add(buttonPlay);
    //Vertical spacing between buttons
    add(Box.createRigidArea(spacingBetweenButtons));
    add(buttonContinue);

    add(Box.createRigidArea(spacingBetweenButtons));
    add(buttonOptions);

    add(Box.createRigidArea(spacingBetweenButtons));
    add(buttonExit);
    //Adding glue to force buttons away from bottom of panel
    add(Box.createVerticalGlue());

    //Aligning all buttons to centered horizontally
    buttonPlay.setAlignmentX(Box.CENTER_ALIGNMENT);
    buttonContinue.setAlignmentX(Box.CENTER_ALIGNMENT);
    buttonOptions.setAlignmentX(Box.CENTER_ALIGNMENT);
    buttonExit.setAlignmentX(Box.CENTER_ALIGNMENT);

    //Setting button sizes
    buttonPlay.setMaximumSize(menuButtonSize);
    buttonContinue.setMaximumSize(menuButtonSize);
    buttonOptions.setMaximumSize(menuButtonSize);
    buttonExit.setMaximumSize(menuButtonSize);

}

}

实际上,如果我使用 titleImage = 和 add(new JLabel) 行,一切都会恢复正常

我猜你只是把路径弄错了——一个常见的错误。在那种情况下,你 应该 得到一个例外,如:

Exception in thread "main" javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at jonathanrmiproject.MyProject.main(JonathanRmiProject.java:24)

这个简单的例子对我有用:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.naming.NamingException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MyProject {
    public static void main(String[] args) throws NamingException, IOException {
        BufferedImage img = ImageIO.read(new File("myimage.jpg"));
        JLabel label = new JLabel(new ImageIcon(img));
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(label);
        f.pack();
        f.setLocation(200, 200);
        f.setVisible(true);
    }
}

我正在使用 Netbeans IDE,我已将图像文件保存到 "C:\Users\David.Sharpe\MyProject\myimage.jpg"。

我要补充一点,如果不是这种情况,并且您确实有正确的路径,那么您需要post一个更详细的问题,以便有人可以帮你。包括重现问题的代码。

更新:从字面上看,它没有出现的原因是因为我太早调用了 setVisble(true)。我不敢相信这是那么简单的事情。我想这可以解释为什么有一次一切都会出现并且一切都很好,但每次都没有出现。 所以在我的构造函数中我有

public class MouseClickGame 扩展 JFrame {

public MouseClickGame() {
    super("WELCOME :D");
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocation(middleOfScreen);
    setVisible(true);
    add(new MouseClickPanel());


}

public static void main(String[] args) {

    //Calling constructor
    MouseClickGame mainClickGame = new MouseClickGame();

}

}

当我所要做的就是将 setVisble() 放在 add(newMouseClickPanel()) 之后

谢谢 DavidS 的建议 :D