展开全部
一楼说得对,我给你代码吧,你写个窗口把这个JPanel放进去就行:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
public class PicPanel extends JPanel
{
BufferedImage img;
public PicPanel(String path)
{
super();
try
{
img = ImageIO.read(new File(path));
}
catch (Exception e)
{
System.out.println("Error: "+e.toString());
}
}
public void paintComponent(Graphics g)//绘制图片的方法
{
try
{
Rectangle rect = new Rectangle(0,0,img.getWidth(null),img.getHeight(null));
TexturePaint texture = new TexturePaint(img,rect);
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(texture);
g2.fillRect(0,0,this.getWidth(),this.getHeight());
super.paintComponent(g);
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
public void setImage(String path)//更换图片的方法
{
try
{
img = ImageIO.read(new File(path));
this.repaint();
}
catch (Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
public class PicPanel extends JPanel
{
BufferedImage img;
public PicPanel(String path)
{
super();
try
{
img = ImageIO.read(new File(path));
}
catch (Exception e)
{
System.out.println("Error: "+e.toString());
}
}
public void paintComponent(Graphics g)//绘制图片的方法
{
try
{
Rectangle rect = new Rectangle(0,0,img.getWidth(null),img.getHeight(null));
TexturePaint texture = new TexturePaint(img,rect);
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(texture);
g2.fillRect(0,0,this.getWidth(),this.getHeight());
super.paintComponent(g);
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
public void setImage(String path)//更换图片的方法
{
try
{
img = ImageIO.read(new File(path));
this.repaint();
}
catch (Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
展开全部
设计一个类继承一个面板JPanel,然后重写其paintComponent(Graphics g)方法,在其内绘制一个图片,然后把它加到窗口中就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
同意一楼观点
我就那样做的,想自定义已有的,就继承重写它里面的方法就是了
我就那样做的,想自定义已有的,就继承重写它里面的方法就是了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
高
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询