java中如何在JPanel里设置背景
1个回答
展开全部
使用成员函数setBackground(Color bg),下面是java文档中对该方法的说明。
public void setBackground(Color bg)
Sets the background color of this component. The background color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations. Direct subclasses of JComponent must override paintComponent to honor this property.
It is up to the look and feel to honor this property, some may choose to ignore it.
public void setBackground(Color bg)
Sets the background color of this component. The background color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations. Direct subclasses of JComponent must override paintComponent to honor this property.
It is up to the look and feel to honor this property, some may choose to ignore it.
追问
如果我加图片哪?
追答
你可以写一个类继承自JPanel,然后在其paint()方法中加入图片。
public void paint(Graphics g)
{
g.drawImage(image,0,0,getWidth(),getHeight(),this);
super.paint(g);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询