2010-06-18 130 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順序閱讀起來。簡單地說,最後添加的組件首先被繪製。