请问如何给我的面板添加一个背景图片啊 想让背景漂亮些,给个思路就行. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 外层容器背景一般可以设计。对于一般控件,重量控件重载其paint方法,轻量控件重载其paintComponent方法更深入的方法可以修改对应控件的默认UI 下面的东东完全可以满足你的要求,注释已经写的相当清楚了,继承自JPanel,所以可以像JPanel那样用它,然后再利用扩展的属性设置你想要的东东。import java.awt.Graphics;import java.awt.Image;import javax.swing.JPanel;/** * 可设置背景图片的JPanel,提供了三种显示背景图片的方式:居中、平铺和拉伸。 * 未设置背景图片的情况下,同JPanel。 * * @author 003 */public class JImagePane extends JPanel{ private static final long serialVersionUID = -8251916094895167058L; /** * 居中 */ public static final String CENTRE = "Centre"; /** * 平铺 */ public static final String TILED = "Tiled"; /** * 拉伸 */ public static final String SCALED = "Scaled"; /** * 背景图片 */ private Image backgroundImage; /** * 背景图片显示模式 */ private String imageDisplayMode; /** * 背景图片显示模式索引(引入此属性有助于必要时扩展) */ private int modeIndex; /** * 构造一个没有背景图片的JImagePane */ public JImagePane() { this(null, CENTRE); } /** * 构造一个具有指定背景图片和指定显示模式的JImagePane * @param image 背景图片 * @param modeName 背景图片显示模式 */ public JImagePane(Image image, String modeName) { super(); setBackgroundImage(image); setImageDisplayMode(modeName); } /** * 设置背景图片 * @param image 背景图片 */ public void setBackgroundImage(Image image) { this.backgroundImage = image; this.repaint(); } /** * 获取背景图片 * @return 背景图片 */ public Image getBackgroundImage() { return backgroundImage; } /** * 设置背景图片显示模式 * @param modeName 模式名称,取值仅限于ImagePane.TILED ImagePane.SCALED ImagePane.CENTRE */ public void setImageDisplayMode(String modeName) { if(modeName != null) { modeName = modeName.trim(); //居中 if(modeName.equalsIgnoreCase(CENTRE)) { this.imageDisplayMode = CENTRE; modeIndex = 0; } //平铺 else if(modeName.equalsIgnoreCase(TILED)) { this.imageDisplayMode = TILED; modeIndex = 1; } //拉伸 else if(modeName.equalsIgnoreCase(SCALED)) { this.imageDisplayMode = SCALED; modeIndex = 2; } this.repaint(); } } /** * 获取背景图片显示模式 * @return 显示模式 */ public String getImageDisplayMode() { return imageDisplayMode; } /** * 绘制组件 * @see javax.swing.JComponent#paintComponent(Graphics) */ @Override protected void paintComponent(Graphics g) { super.paintComponent(g); //如果设置了背景图片则显示 if(backgroundImage != null) { int width = this.getWidth(); int height = this.getHeight(); int imageWidth = backgroundImage.getWidth(this); int imageHeight = backgroundImage.getHeight(this); switch(modeIndex) { //居中 case 0: { int x = (width - imageWidth) / 2; int y = (height - imageHeight) / 2; g.drawImage(backgroundImage, x, y, this); break; } //平铺 case 1: { for(int ix = 0; ix < width; ix += imageWidth) { for(int iy = 0; iy < height; iy += imageHeight) { g.drawImage(backgroundImage, ix, iy, this); } } break; } //拉伸 case 2: { g.drawImage(backgroundImage, 0, 0, width, height, this); break; } } } }} import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import javax.swing.JPanel;import mmlab.image.Rectangle;/** * Custom Jpanel * * @author Andy Yang */public class FaceLabel extends JPanel { private static final long serialVersionUID = 39082560987930759L; private boolean state = false; private Rectangle rectangle = null; private Image image = null; /** 记录鼠标是否进入控件 */ private boolean hover; private String personName = null; private double photoScaled = 1.0; /** * 无参构造方法 */ public FaceLabel() { } /** * 带有Color的构造方法 * * @param color * 背景颜色 */ public FaceLabel(Image image) { this.image = image; // 定义图像尺寸 Dimension size = new Dimension(this.image.getWidth(null), this.image.getHeight(null)); setPreferredSize(size); setMinimumSize(size); setMaximumSize(size); setSize(size); setBackground(new Color(112, 112, 112)); // 定义布局方式为空 setLayout(null); } /** * 重写基类的无参构造方法 */ @Override protected void paintComponent(Graphics g) { setOpaque(true); super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.drawImage(this.getImage(), 0, 0, null); if (isState() && (getRectangle() != null)) { // JOptionPane.showMessageDialog(null,getPhotoScaled()); Rectangle rect = getRectangle(); int x = rect.x; int y = rect.y; int width = rect.width; int height = rect.height; if (getPhotoScaled() == 1.0) { g2d.setColor(new Color(255, 0, 0)); g2d.drawRect(x, y, width, height); // g2d.draw3DRect(x,y,width,height,true); g2d.setColor(new Color(255, 255, 0)); g2d.drawString(this.getPersonName(), x, y - 5); } else { String sX = String.valueOf(Math.ceil( (double) rect.x / photoScaled)); String sY = String.valueOf(Math.ceil( (double) rect.y / photoScaled)); String sW = String.valueOf(Math.ceil( (double) rect.width / photoScaled)); String sH = String.valueOf(Math.ceil( (double) rect.height / photoScaled)); int indexX = sX.indexOf("."); int indexY = sY.indexOf("."); int indexW = sW.indexOf("."); int indexH = sH.indexOf("."); x = Integer.parseInt(sX.substring(0, indexX)); y = Integer.parseInt(sY.substring(0, indexY)); width = Integer.parseInt(sW.substring(0, indexW)); height = Integer.parseInt(sH.substring(0, indexH)); g2d.setColor(new Color(255, 0, 0)); g2d.drawRect(x, y, width, height); // g2d.draw3DRect(x,y,width,height,true); g2d.setColor(new Color(0, 0, 255)); g2d.drawString(this.getPersonName(), x, y - 5); } } } public boolean isState() { return state; } public void setState(boolean state) { this.state = state; } public Rectangle getRectangle() { return rectangle; } public void setRectangle(Rectangle rectangle) { this.rectangle = rectangle; } public Image getImage() { return image; } public void setImage(Image image) { this.image = image; } public String getPersonName() { return (personName == null) ? "null" : personName; } public void setPersonName(String personName) { this.personName = personName; } public double getPhotoScaled() { return photoScaled; } public void setPhotoScaled(double photoScaled) { this.photoScaled = photoScaled; }}//这是我项目中用到的一个控件.你自己试着去改一下吧.//关键代码是: /** * 重写基类的无参构造方法 */ @Override protected void paintComponent(Graphics g) { setOpaque(true); super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.drawImage(this.getImage(), 0, 0, null); } 写两个六楼那个类的用法吧JImagePane panel = new JImagePane(new ImageIcon("003.png").getImage(), JImagePane.SCALED);//或者JImagePane panel = new JImagePane();panel.setBackgroundImage(new ImageIcon("003.png").getImage());panel.setImageDisplayMode(JImagePane.SCALED); 用Image然后读进要装载图片image = getImage(getCodeBase(),"firstImage.jpg最后在定义的面板上画出图片g.drawImage(Image,10,10,200,200,this) 1. 先创建一个Image对象2. 把这个Image画到面板上, 从(0, 0) 到(面板的width, 面板的height).public class MyPanel extends JPanel { // 定义图像的引用 Image bgImage; // 步骤 1 public MyPanel() { bgImage = new ImageIcon("..../bg.png").getImage(); // 创建图像对象. } // 步骤 2 protected void paintComponent(Graphics g) { super.paintComponent(g); if (bgImage != null) { g.drawImage(bgImage, 0, 0, this.getWidgth(), this.getHeight(), null); } }}然后使用这个MyPanel就像使用普通的JPanel一样, 直接使用add加入组件等, 没有任何区别.MyPanel panel = new MyPanel();panel.add(new JButton("Button"));.............. 如何定义一个常量数组? JVM调优的问题 java swing简繁体中文显示乱码问题 java 环境变量配置问题 请问这个this是什么意思 请教:命令行传递字符的问题? 关于数据字典???? 请大家帮忙 我是搞C++的!现在想搞JAVA!于是想问: JDK, JSDK ,JRE ,它们是搞什么作用的,难道它没有像C++那样有集成环境吗,还有.......(内容) 请问各位大峡 正则表达式 的反向引用问题 StringBuffer 中append的问题!编译通过,执行报错,救急!
对于一般控件,重量控件重载其paint方法,轻量控件重载其paintComponent方法更深入的方法可以修改对应控件的默认UI
import java.awt.Graphics;
import java.awt.Image;import javax.swing.JPanel;/**
* 可设置背景图片的JPanel,提供了三种显示背景图片的方式:居中、平铺和拉伸。
* 未设置背景图片的情况下,同JPanel。
*
* @author 003
*/
public class JImagePane extends JPanel
{
private static final long serialVersionUID = -8251916094895167058L;
/**
* 居中
*/
public static final String CENTRE = "Centre";
/**
* 平铺
*/
public static final String TILED = "Tiled"; /**
* 拉伸
*/
public static final String SCALED = "Scaled"; /**
* 背景图片
*/
private Image backgroundImage;
/**
* 背景图片显示模式
*/
private String imageDisplayMode; /**
* 背景图片显示模式索引(引入此属性有助于必要时扩展)
*/
private int modeIndex; /**
* 构造一个没有背景图片的JImagePane
*/
public JImagePane()
{
this(null, CENTRE);
}
/**
* 构造一个具有指定背景图片和指定显示模式的JImagePane
* @param image 背景图片
* @param modeName 背景图片显示模式
*/
public JImagePane(Image image, String modeName)
{
super();
setBackgroundImage(image);
setImageDisplayMode(modeName);
}
/**
* 设置背景图片
* @param image 背景图片
*/
public void setBackgroundImage(Image image)
{
this.backgroundImage = image;
this.repaint();
} /**
* 获取背景图片
* @return 背景图片
*/
public Image getBackgroundImage()
{
return backgroundImage;
} /**
* 设置背景图片显示模式
* @param modeName 模式名称,取值仅限于ImagePane.TILED ImagePane.SCALED ImagePane.CENTRE
*/
public void setImageDisplayMode(String modeName)
{
if(modeName != null)
{
modeName = modeName.trim();
//居中
if(modeName.equalsIgnoreCase(CENTRE))
{
this.imageDisplayMode = CENTRE;
modeIndex = 0;
}
//平铺
else if(modeName.equalsIgnoreCase(TILED))
{
this.imageDisplayMode = TILED;
modeIndex = 1;
}
//拉伸
else if(modeName.equalsIgnoreCase(SCALED))
{
this.imageDisplayMode = SCALED;
modeIndex = 2;
}
this.repaint();
}
} /**
* 获取背景图片显示模式
* @return 显示模式
*/
public String getImageDisplayMode()
{
return imageDisplayMode;
} /**
* 绘制组件
* @see javax.swing.JComponent#paintComponent(Graphics)
*/
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
//如果设置了背景图片则显示
if(backgroundImage != null)
{
int width = this.getWidth();
int height = this.getHeight();
int imageWidth = backgroundImage.getWidth(this);
int imageHeight = backgroundImage.getHeight(this); switch(modeIndex)
{
//居中
case 0:
{
int x = (width - imageWidth) / 2;
int y = (height - imageHeight) / 2;
g.drawImage(backgroundImage, x, y, this);
break;
}
//平铺
case 1:
{
for(int ix = 0; ix < width; ix += imageWidth)
{
for(int iy = 0; iy < height; iy += imageHeight)
{
g.drawImage(backgroundImage, ix, iy, this);
}
}
break;
}
//拉伸
case 2:
{
g.drawImage(backgroundImage, 0, 0, width, height, this);
break;
}
}
}
}
}
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;import javax.swing.JPanel;import mmlab.image.Rectangle;
/**
* Custom Jpanel
*
* @author Andy Yang
*/
public class FaceLabel extends JPanel {
private static final long serialVersionUID = 39082560987930759L;
private boolean state = false;
private Rectangle rectangle = null;
private Image image = null; /** 记录鼠标是否进入控件 */
private boolean hover;
private String personName = null;
private double photoScaled = 1.0; /**
* 无参构造方法
*/
public FaceLabel() {
} /**
* 带有Color的构造方法
*
* @param color
* 背景颜色
*/
public FaceLabel(Image image) {
this.image = image; // 定义图像尺寸
Dimension size = new Dimension(this.image.getWidth(null),
this.image.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setBackground(new Color(112, 112, 112));
// 定义布局方式为空
setLayout(null);
} /**
* 重写基类的无参构造方法
*/
@Override
protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(this.getImage(), 0, 0, null); if (isState() && (getRectangle() != null)) {
// JOptionPane.showMessageDialog(null,getPhotoScaled());
Rectangle rect = getRectangle();
int x = rect.x;
int y = rect.y;
int width = rect.width;
int height = rect.height; if (getPhotoScaled() == 1.0) {
g2d.setColor(new Color(255, 0, 0));
g2d.drawRect(x, y, width, height);
// g2d.draw3DRect(x,y,width,height,true);
g2d.setColor(new Color(255, 255, 0));
g2d.drawString(this.getPersonName(), x, y - 5);
} else {
String sX = String.valueOf(Math.ceil(
(double) rect.x / photoScaled));
String sY = String.valueOf(Math.ceil(
(double) rect.y / photoScaled));
String sW = String.valueOf(Math.ceil(
(double) rect.width / photoScaled));
String sH = String.valueOf(Math.ceil(
(double) rect.height / photoScaled));
int indexX = sX.indexOf(".");
int indexY = sY.indexOf(".");
int indexW = sW.indexOf(".");
int indexH = sH.indexOf(".");
x = Integer.parseInt(sX.substring(0, indexX));
y = Integer.parseInt(sY.substring(0, indexY));
width = Integer.parseInt(sW.substring(0, indexW));
height = Integer.parseInt(sH.substring(0, indexH));
g2d.setColor(new Color(255, 0, 0));
g2d.drawRect(x, y, width, height);
// g2d.draw3DRect(x,y,width,height,true);
g2d.setColor(new Color(0, 0, 255));
g2d.drawString(this.getPersonName(), x, y - 5);
}
}
} public boolean isState() {
return state;
} public void setState(boolean state) {
this.state = state;
} public Rectangle getRectangle() {
return rectangle;
} public void setRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
} public Image getImage() {
return image;
} public void setImage(Image image) {
this.image = image;
} public String getPersonName() {
return (personName == null) ? "null" : personName;
} public void setPersonName(String personName) {
this.personName = personName;
} public double getPhotoScaled() {
return photoScaled;
} public void setPhotoScaled(double photoScaled) {
this.photoScaled = photoScaled;
}
}
//这是我项目中用到的一个控件.你自己试着去改一下吧.//关键代码是: /**
* 重写基类的无参构造方法
*/
@Override
protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(this.getImage(), 0, 0, null); }
JImagePane panel = new JImagePane(new ImageIcon("003.png").getImage(), JImagePane.SCALED);
//或者
JImagePane panel = new JImagePane();
panel.setBackgroundImage(new ImageIcon("003.png").getImage());
panel.setImageDisplayMode(JImagePane.SCALED);
然后读进要装载图片image = getImage(getCodeBase(),"firstImage.jpg
最后在定义的面板上画出图片
g.drawImage(Image,10,10,200,200,this)
2. 把这个Image画到面板上, 从(0, 0) 到(面板的width, 面板的height).public class MyPanel extends JPanel {
// 定义图像的引用
Image bgImage; // 步骤 1
public MyPanel() {
bgImage = new ImageIcon("..../bg.png").getImage(); // 创建图像对象.
} // 步骤 2
protected void paintComponent(Graphics g) {
super.paintComponent(g); if (bgImage != null) {
g.drawImage(bgImage, 0, 0, this.getWidgth(), this.getHeight(), null);
}
}}然后使用这个MyPanel就像使用普通的JPanel一样, 直接使用add加入组件等, 没有任何区别.
MyPanel panel = new MyPanel();
panel.add(new JButton("Button"));
..............