JPanel jp=new JPanel()://定义面板并初始化
Icon iocn=new ImageIcon("C:/My Documents/tupian.jpg")//定义图片并初始化,写上图片的绝对路径
JLabel jl=new JLabel(icon)://把图片放在标签上
jp.add(jl)//往面板上添加标签注意:面板JPanel不能之间添加图片iocn,icon需要放在标签JLabel上,才能在JPanel上显示
import java.awt.event.*
import javax.swing.*
public class MainJFrame extends JFrame{
private ImageJPanel ip
public MainJFrame() {
initial()
}
public void initial()
{
ip=new ImageJPanel()
this.setTitle("Demo")
this.setSize(400,300)
this.setResizable(false)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
this.setLocationRelativeTo(this)
Container c=this.getContentPane()
c.setLayout(null)
ip.setBounds(0,0,this.getWidth(),this.getHeight())
c.add(ip)
this.setVisible(true)
}
public static void main(String[] args)
{
new MainJFrame()
}
}
class ImageJPanel extends JPanel
{
private ImageIcon ii
public ImageJPanel()
{
//bk.jpg是指背景图片的名称 ii=new ImageIcon("bk.jpg")
}
//绘制背景图片 public void paintComponent(Graphics g)
{
super.paintComponent(g)
g.drawImage(ii.getImage(),0,0,this)
}
}
1、在Body中添加<html>
<head><title></title></head>
<body background="背景图片地址">
</body>
</html>
2、在登录框的div添加
<div id="login" style="background-image: 背景图片地址">
3、在css添加
在body添加:
BODY {
background-image: 图片路径地址
}
根据ID添加
#login{
background-image: 图片路径地址
}
这些都可以的,还有js也可以添加的。
我直接给你一个ImagePanel类,你自己改改。public class ImagePanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L
protected BufferedImage image
protected Rectangle rectangle=new Rectangle()
public ImagePanel() {
setLayout(null)
installListeners()
}
private void installListeners() {
}
public void paintComponent(Graphics g) {
super.paintComponent(g)
if(image==null){
return
}
g.drawImage(image, 0, 0, null)
if(rectangle.width>=0&&rectangle.height>=0){
Color c = g.getColor()
g.setColor(Color.RED)
g.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height)
g.setColor(c)
}
}
public BufferedImage getImage() {
return image
}
public void setImage(BufferedImage image) {
this.setImage(image, true)
}
public void setImage(BufferedImage image, boolean repaint) {
this.image = image
if (image != null) {
Dimension size = new Dimension(image.getWidth(null),image.getHeight(null))
setPreferredSize(size)
setMinimumSize(size)
setMaximumSize(size)
setSize(size)
} else {
}
rectangle=new Rectangle()
if (repaint)
this.repaint()
}
public boolean isDirty() {
return image != null
}
/*
public void updateCrosshair(int i) {
y=i
repaint()
}*/
public void updateZooe(Rectangle r) {
this.rectangle=r
repaint()
}
}
核心代码就是重写paintComponent。再者你的代码习惯不好,布局管理器用的不科学。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)