你说的如果是java swing程序,有两种方法,1、JPanel上放背景;2、JLabel放背景
// 初始化窗口public void initFrame() {
// 利用JPanel添加背景图片
GImage = new JPanel() {
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon("image\\benbenla.jpg")
Image img = icon.getImage()
g.drawImage(img, 0, 0, icon.getIconWidth(),
icon.getIconHeight(), icon.getImageObserver())
jframe.setSize(icon.getIconWidth(), icon.getIconHeight())
}
}
jframe.setTitle("测试背景图片")
jframe.add(GImage)
jframe.pack()
jframe.setVisible(true)
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
} ImageIcon icon = new ImageIcon("image\\benbenla.jpg")
icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),
icon.getIconHeight(), Image.SCALE_DEFAULT))
System.out.println(icon.getIconHeight() + "" + icon.getIconWidth())
jlpic.setBounds(0, 0, 1366, 768)
jlpic.setHorizontalAlignment(0)
jlpic.setIcon(icon)
jframe.setSize(1366, 768)
jframe.add(jlpic)
jframe.pack()
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
jframe.setVisible(true)
1)文件要有后缀名2)要用Image或BufferedImage对象
3)因为你重写了paint()方法,所以不能在Label里面显示图片。你重写了paint()方法后,整个容器都会变成画布,所以看不到Label组件,自然也就看不到图片。应该在paint方法里面用g.drawImage方法把图片在画布中画出来。参考Java API,Graphics的drawImage方法。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)