2010-06-18 39 views
1

我有一条道路的背景图像,我使用ImageIcon在JFrame上显示道路。我想在某个(x,y)位置放置一辆汽车在该背景图像的顶部。如何在java中放置图像彼此顶部

我试过使用另一个ImageIcon并且当我运行该程序时背景没有出现,但是汽车没有。

import javax.swing.ImageIcon; 

import javax.swing.JFrame; 

import javax.swing.JLabel; 

public class Gui extends JFrame { 

private ImageIcon northcar = new ImageIcon("src/north.gif"); 
private ImageIcon usIcon = new ImageIcon("src/trafficLight.jpg"); 


public Gui() { 
    add(new JLabel(usIcon)); // background image does not appear after the i added the northcar label 
    add(new JLabel(northcar)); // this picture can be seen 


} 

public static void main(String[] args) { 


    Gui frame = new Gui(); 
    frame.setTitle("TestImageIcon"); 
    frame.setLocationRelativeTo(null); // Center the frame 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setBounds(0, 0, 650, 650); 
    frame.setVisible(true); 

} 
} 

我听说过使用画布,但没有任何线索。有任何想法吗 ?

非常感谢

回答

1

如何使用LayeredPanethis可能的帮助。

+0

作品魅力m8 – Haxed 2010-06-18 06:24:11

0
add(new JLabel(usIcon)); 
add(new JLabel(northcar)); 

更改为了:

add(new JLabel(northcar)); 
add(new JLabel(usIcon)); 

z顺序阅读起来。简单地说,最后添加的组件首先被绘制。

相关问题