怎么在Java里添加背景图片

怎么在Java里添加背景图片,第1张

可以新建面板,在面板里放入带图片的JLabel,填满面板即可。

JPanel jp = new JPanel()//新建面板

jp.setLayout(new FlowLayout()) //设置面板布局

ImageIcon ii=new ImageIcon(getClass().getResource("/Picture/i.jpg"))

JLabel uppicture=new JLabel(ii)//往面板里加入JLabel

this.setVisible(true)

// 不加包,图片跟类文件在一个目录,命令行下编译执行就行了

// 如果建工程,图片放到工程根目录

import java.awt.Container

import java.awt.FlowLayout

import javax.swing.ImageIcon

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JPanel

public class Student extends JFrame {

public Student() {

init()

}

private void init() {

final Container c = getContentPane()

final JLabel imgLabel = new JLabel()

ImageIcon img = new ImageIcon(System.getProperty("user.dir") + "\\a.png")

imgLabel.setIcon(img)

imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight())

((JPanel)getContentPane()).setOpaque(false)

getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE))

setLayout(new FlowLayout())

setSize(500, 500)

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

}

public static void main(String[] args) {

new Student().setVisible(true)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11588398.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存