如何在java中插入图片

如何在java中插入图片,第1张

public Qua_Main_JFrame() {

JPanel jpanel = new JPanel()

this.setContentPane(jpanel)

//

添加标签组件

GridLayout gird = new GridLayout(3,0)

jpanel.setLayout(gird)

ImageIcon img = new ImageIcon("src/JMXY.JPG")

JLabel imgLabel = new JLabel(img)//

将背景图放在标签里。

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

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

this.getLayeredPane().setLayout(null)

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)

jpanel.setOpaque(false)

initComponents()

}

其中

Qua_Main_JFrame

为创建的

java

窗体项目名,图片路径存放,注意一点,所有

的代码都应该放在

initComponents()

方法之上,这样你添加进窗体中的空间才会显示在

图片之上,否则看不见控件。

图片路径可能不对 

ImageIcon myIcon = new ImageIcon("src/image/black.jpg")

你这样写试试

通过以下方式设置下背景就可以了:

background = new ImageIcon("images/backImage.png")

backImage = new JLabel(background)

backImage.setBounds(0, 0, background.getIconWidth(),

background.getIconHeight())

backPanel = (JPanel) this.getContentPane()

backPanel.setOpaque(false)

this.getLayeredPane().setLayout(null)

this.getLayeredPane().add(backImage, new Integer(Integer.MIN_VALUE))

backPanel.setLayout(new BorderLayout())

这样就可以把图片放在最下面了,效果图:


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存