如何在Java中将图像带到前台? [英] How to bring an image to the foreground in Java?
问题描述
这是代码:
JFrame window = new JFrame();
ImageImplement pacman = new ImageImplement(new ImageIcon(C:\\\\ Users \\\\\\\\\\\\\\\\\\\\\\\\'')。getImage() );
ImageImplement panel = new ImageImplement(new ImageIcon(C:\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\')。getImage());
pacman.setLocation(255,255);
pacman.setVisible(true);
pacman.setOpaque(true);
window.add(pacman);
window.add(panel);
window.setVisible(true);
window.setSize(576,655);
window.setName(Pacman);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
直接使用窗口的JLayeredPane作为容器并在添加组件时指定组件的深度(z顺序)。
I'm trying to create a Pacman game in Java, and so far I've displayed the background image (the blue and black maze), but I'm having trouble showing the image of Pacman. When I try to display him in the same method with which I displayed the background, he doesn't appear unless I slightly manually alter the size of the Jframe. And even then when he appears there's a small white square in the bottom right corner of his image. What can I do to fix this? Is there any other way I can insert the pacman image in that works?
This is the code:
JFrame window = new JFrame();
ImageImplement pacman = new ImageImplement(new ImageIcon("C:\\Users\\16ayoubc\\Desktop\\Pacman-moving.gif").getImage());
ImageImplement panel = new ImageImplement(new ImageIcon("C:\\Users\\16ayoubc\\Desktop\\background.png").getImage());
pacman.setLocation(255, 255);
pacman.setVisible(true);
pacman.setOpaque(true);
window.add(pacman);
window.add(panel);
window.setVisible(true);
window.setSize(576,655);
window.setName("Pacman");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Use a JLayeredPane insted of the window directly for a container and specify the depth (z-order) of the components when you add them.
这篇关于如何在Java中将图像带到前台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!