JPanel不显示背景图像 [英] JPanel not showing background image
本文介绍了JPanel不显示背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个非常简单的程序,我已经尽力了,但是JPanel没有提供背景图像.我只希望面板上有一个简单的背景图像.
It is very simple program and i have tried my best but the JPanel doesn't come up with a background image. I just want a simple background image on my panel.
这是我的代码:
import javax.swing.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.Graphics;
class PanelEx extends JPanel
{
BufferedImage img;
PanelEx()
{
try
{
img = ImageIO.read(new File("C:/Users/Pictures/s_4261.jpg"));
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void printComponent(Graphics g)
{
g.drawImage(img,0,0,null);
}
}
class JPanelEx2 extends JFrame
{
PanelEx pe;
public static void main(String args[])
{
new JPanelEx2();
}
JPanelEx2()
{
pe = new PanelEx();
add(pe);
setTitle("JPanel Title");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(320,240);
setVisible(true);
}
}
预先感谢
推荐答案
替换
public void printComponent(Graphics g) {
使用
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
这篇关于JPanel不显示背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文