如何在java中插入图片

Python018

如何在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()

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

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

可以新建个面板,在面板里放入带图片的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)