JPanel上能添加背景图片吗

wenbo234 2007-10-30 07:46:40
JPanel里有个setBackground()方法可以改变JPanel的背景颜色 但是就换下颜色还是很丑陋
我想在JPanel上添加个图片作为背景 请教各位高手怎么做啊 小弟菜鸟啊 ~~!!!!!在线等啊 求助各位大哥
...全文
2156 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wenbo234 2007-10-31
  • 打赏
  • 举报
回复
谢谢5楼6楼的兄弟 我明白了 
springking11 2007-10-31
  • 打赏
  • 举报
回复
java要加背景图比较麻烦,需要重载paintComponent方法,下面以JPanel为例,其它按此继承就可
JPanel jContentPane = new getpane(800,600,"images\\0044.jpg");
jContentPane.setLayout(null);
jContentPane.setOpaque(false);//此处要设为false
jContentPane.setAlignmentY(0.5F);
jContentPane.add(jLabel, null);
jContentPane.add(getJTestYear(), null);
this.initComboBox();
jContentPane.add(getJSearchByName(), null);
jContentPane.add(getJSearchByID(), null);
jContentPane.add(getJIDOrName(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJPanel(), null);
this.setContentPane(jContentPane);

class getpane extends JPanel{
private static final long serialVersionUID = 1L;
int width=0,hight=0;
String imgpath = "";
public getpane(int width,int hight,String file){
this.width = width;
this.hight = hight;
imgpath = file;
}
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon(imgpath);
Image img = icon.getImage();
g.drawImage(img, 0, 0, width, hight, this);
}

}
kshan 2007-10-30
  • 打赏
  • 举报
回复
这里有个参考例子

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import java.awt.BorderLayout;
import java.awt.Graphics;


public class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
ImageIcon image=new ImageIcon(Frame1.class.getResource(
"a.jpg"));
Panel1 jp=new Panel1(image);
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
contentPane.add(jp);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
}
}


class Panel1 extends JPanel {
BorderLayout borderLayout1 = new BorderLayout();
private ImageIcon image = null;
public Panel1(ImageIcon image) {
try {
this.image = image;
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
this.setLayout(borderLayout1);
}
protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g);
Dimension d = getSize();
for( int x = 0; x < d.width; x += image.getIconWidth() )
for( int y = 0; y < d.height; y += image.getIconHeight() )
g.drawImage( image.getImage(), x, y, null, null );
}
}
wenbo234 2007-10-30
  • 打赏
  • 举报
回复
具体怎么做呀?再说详细一点吧
g376983529 2007-10-30
  • 打赏
  • 举报
回复
实列化ImageIcon类就可以了
wenbo234 2007-10-30
  • 打赏
  • 举报
回复
我晕这个不是背景图片呀 难道是把JLabel拉到足够大 fill整个面板吗? 那其他的东西不能放了啊
math_man 2007-10-30
  • 打赏
  • 举报
回复
在JLabel上面添加图片,然后将JLabel添加到JPanel中就可以了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧