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

时间:2015-07-28 21:20:56

标签: image jframe load imageicon

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

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

在我运行程序后,我的整个框架变成空白,而在我之前有一些JButton。我在try-catch行之前的代码工作得非常好并且我测试看到唯一能打破它的是这行代码。我没有收到错误或任何东西,我在我的项目文件夹中有图像,似乎图像加载正常,除了它不会出现在框架上,框架上的其他一切都消失了。我只是不明白为什么它在我加载图像时会清除整个画面。

以下是完整代码:

这是扩展JFrame

的类

package mouse.click.game;

import java.awt.Dimension;

import java.awt.Point;

import java.awt.Toolkit;

导入javax.swing.JFrame;

公共类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的类(这是我项目中唯一的两个类)

package mouse.click.game;

import java.awt.Dimension;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

导入javax.imageio.ImageIO;

导入javax.swing.Box;

导入javax.swing.BoxLayout;

导入javax.swing.ImageIcon;

导入javax.swing.JButton;

导入javax.swing.JLabel;

导入javax.swing.JPanel;

公共类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 =并添加(新的JLabel)行,一切都恢复正常

2 个答案:

答案 0 :(得分:0)

我的猜测是你错了路 - 这是一个常见的错误。在这种情况下,您应该获得如下的异常:

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"。

我应该补充说,如果这是的情况,并且你确实有正确的路径,那么你需要发布一个更详细的问题,以便有人可以帮助你。包括重现问题的代码。

答案 1 :(得分:0)

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

公共类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();

}

}

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

感谢DavidS的建议:D