把一个图片设置成Frame的背景,该重写哪个方法?
我试着重写了paint(Graphics)方法,图片是设成背景了,不过上面的Button等组件就看不到了.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Back extends JFrame
{
ImageIcon img;
public Back()throws Exception
{
super( "abc ");
setLayout(new FlowLayout());
add(new JButton( "skajdfsd "));
img=new ImageIcon(getClass().getResource( "2.jpg "));
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
pack();
System.out.println(img.getIconWidth()+ " "+img.getIconHeight());
setVisible(true);
}
public void paint(Graphics g)
{
super.setSize(216,244);
g.drawImage(img.getImage(),0,0,getWidth(),getHeight(),this);
}
public static void main(String...dd) throws Exception
{
new Back();
}
}
------解决方案--------------------panel上面考虑一下