Java 在Eclipse中将图像添加到JPanel
我的主要课程中有:Java 在Eclipse中将图像添加到JPanel,java,image,swing,background,jpanel,Java,Image,Swing,Background,Jpanel,我的主要课程中有: panel.setBackground(Color.green); ImagePanel background = new ImagePanel("Images/background.png"); panel.add(background); 但当我运行它时,我只看到绿色背景,并得到异常: “javax.imageio.IIOException:无法读取输入文件!” 这是ImagePanel类: public class ImagePanel extends JPanel
panel.setBackground(Color.green);
ImagePanel background = new ImagePanel("Images/background.png");
panel.add(background);
但当我运行它时,我只看到绿色背景,并得到异常:
“javax.imageio.IIOException:无法读取输入文件!”
这是ImagePanel类:
public class ImagePanel extends JPanel {
private BufferedImage img;
public ImagePanel(String path) {
// load the background image
try {
img = ImageIO.read(new File(path));
} catch(IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// paint the background image and scale it to fill the entire space
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
}
我正在使用Eclipse,这就是我的图像所在的位置:src/Images/background.png
好的,现在我有:
ImagePanel background = new ImagePanel("src/Images/background.png");
它不再显示异常,但我仍然看不到图像,只有绿色背景
以下是完整的方法:
private void createAndShowGUI() {
frame = new JFrame("Java 2048 By Xandru");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLayout(null);
panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, HEIGHT, WIDTH);
panel.setBackground(Color.green);
frame.add(panel);
//Add the background
ImagePanel background = new ImagePanel("src/Images/background.png");
panel.add(background);
//Create the main Frame
frame.pack();
//Set dimensions
frame.setSize(WIDTH, HEIGHT);
//Center it
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((screen.getWidth() - frame.getWidth()) /2);
int y = (int) ((screen.getHeight() - frame.getHeight()) /2);
frame.setLocation(x, y);
//Set visible
frame.setVisible(true);
}
从资源文件夹中查看图像
2.png
Image image= ImageIO.read(new File("resources/2.png"));
或
如果图像与类所在的包(文件夹)相同,请尝试此操作
Image image = ImageIO.read(getClass().getResourceAsStream("2.png"));
这是项目结构
--编辑- 这样试试
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Java 2048 By Xandru");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
// Add the background
ImagePanel background = new ImagePanel("src/images/2.png");
frame.add(background);
// Create the main Frame
frame.pack();
// Set dimensions
frame.setSize(new Dimension(width, height));
// Center it
frame.setLocationRelativeTo(null);
// Set visible
frame.setVisible(true);
}
});
不知道这是否是引起你注意的正确方式,我是stackoverflow的新手。。。无论如何,我已经更新了问题:)没问题。这是正确的方法。欢迎使用StackOverflow。谢谢,这对异常起到了作用,所以现在我已经正确加载了图像,但仍然无法在屏幕上看到它…:(@XandruDavid您可能应该显示如何添加面板不要使用
null
布局。不要使用setBounds
。为什么要在两者之间使用额外的面板
?从设计角度来看,公共图像面板(字符串路径){
应该是公共图像面板(图像图像){
(IMO)。这样,面板可以处理内存中生成的文件、URL或..中的图像。