2012-06-09 47 views
1

我放置兩個圖像。一個應該是背景圖片,另一個應該是一個簡筆畫的圖片。我想在背景前獲得棒圖。我可以通過插入代碼來將代碼後面的背景圖片顯示爲棒圖。我想知道是否有無論如何要完成同樣的事情,通過在背景代碼後面插入棒圖代碼,所以我可以繼續將新的JLabel放在背景之上。將JLabel移動到前面

,工程代碼:

guy.setBounds(0,0,100,100); 
    panel.add(guy); 

    backgroundPic.setBounds(0,0,550,550); 
    panel.add(backgroundPic); 

    setVisible(true); 

,我想工作代碼:

backgroundPic.setBounds(0,0,550,550); 
    panel.add(backgroundPic); 

    guy.setBounds(0,0,100,100); 
    panel.add(guy); 


    setVisible(true); 
+0

您使用的是JLayeredPane的或類似的結構?如果是這樣,你可以確切地說*添加組件到哪個圖層。 –

+0

或?JLayer如果Java 1.7? –

+0

查看[這個問題]的答案(http://stackoverflow.com/questions/3360898/jpanel-component-draw-order),組件的不透明度可以影響z順序。當您重繪其中一個時,這似乎更重要,我認爲這可能是您的使用案例之一。 – GallifreyanCode

回答

1

可以使用Z順序,以表明該組件應呈現的順序:

java.awt.Container.getComponentZOrder(Component) 

根據文檔:

返回容器內組件的z順序索引。一個組件在z順序層級中的位置越高,其索引越低。 具有最低z順序索引的組件繪製爲最後,其餘子組件都是 。

參數:排版組件被查詢

返回:z順序 索引處的分量;否則返回-1,如果組件爲null 或不屬於容器

0

我不會聲稱自己是一個Java專家也不會對我要求這是正確的,只是一種猜測,但嘗試刪除

.setBounds()

,如果有沒有效果,可以嘗試使用

.setSize()

好運:)的

3

如果你只擔心一個背景圖片,那麼我會建議擴展JPanel,重寫paintComponent和油漆的背景圖像。

如果你要關心的幾個部件的Z順序,然後JLayeredPane是您最佳的選擇。

這裏是一個小片段顯示了第一個選項:

import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Point; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class Test2 { 
    private static class PanelWithBackground extends JPanel { 

     private Image backgroundImage; 
     private Point backgroundLocation = new Point(); 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (getBackgroundImage() != null) { 
       g.drawImage(getBackgroundImage(), backgroundLocation.x, backgroundLocation.y, this); 
      } 
     } 

     public Image getBackgroundImage() { 
      return backgroundImage; 
     } 

     public void setBackgroundImage(Image backgroundImage) { 
      this.backgroundImage = backgroundImage; 
      repaint(); 
     } 

     public Point getBackgroundLocation() { 
      return backgroundLocation; 
     } 

     public void setBackgroundLocation(Point backgroundLocation) { 
      this.backgroundLocation = backgroundLocation; 
      repaint(); 
     } 
    } 

    protected static void initUI() throws MalformedURLException { 
     JFrame frame = new JFrame("test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     PanelWithBackground panelWithBackground = new PanelWithBackground(); 
     panelWithBackground.setLayout(null); 
     panelWithBackground.setBackgroundImage(new ImageIcon(new URL(
       "http://2.bp.blogspot.com/-PqKuKt5mRmc/Tvi-K-4FVVI/AAAAAAAACKg/YwzkME5gGvk/s1600/black+background.jpg")).getImage()); 
     JLabel guy = new JLabel(new ImageIcon(new URL(
       "http://www.clipproject.info/Cliparts_Free/Menschen_Free/Clipart-Cartoon-Design-04.gif"))); 
     // Next 2 lines should rather be performed by a LayoutManager 
     panelWithBackground.setPreferredSize(new Dimension(1024, 768)); 
     guy.setBounds(50, 200, guy.getPreferredSize().width, guy.getPreferredSize().height); 

     panelWithBackground.add(guy); 
     frame.add(panelWithBackground); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        initUI(); 
       } catch (MalformedURLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
}