2014-04-07 29 views
4

我的問題是,當向JPanel添加一個.GIF時,它會爲.GIF顯示這個黑色背景。在JPanel上添加一個.GIF的黑色方塊顯示

結果上的JPanel添加時:

http://i.imgur.com/W1HXgZ1.jpg

它發生時,我用這條線:

p2.add(loadBar); // where p2 = new JPanel(); 

然而,當我添加在JFrame的同一.GIF時,黑色方塊不再存在了。就像這樣:

jf.add(loadBar); // where jf = new JFrame(); 

結果在JFrame中添加時:

http://i.imgur.com/0pMAS30.jpg

的類代碼部分:

String loadLink = "http://i.imgur.com/mHm6LYH.gif"; 
     URL ajaxLoad = null; 
     try { 
      ajaxLoad = new URL(loadLink); 
     } catch (MalformedURLException e3) { 
      // TODO Auto-generated catch block 
      e3.printStackTrace(); 
     } 
     ImageIcon loading = new ImageIcon(ajaxLoad); 
     JLabel loadBar = new JLabel(loading); 
     loadBar.setBounds(70, 60, 54, 55); 
     loadBar.setOpaque(false); 
     //jf.add(loadBar); 
     p2.add(loadBar); 

可能有人請解釋爲什麼它正在發生喜歡這個?感謝您的時間和閱讀。

編輯:

// Creates the Initialization Panel 
     p2 = new JPanel(); 
     // Sets the background, black with 125 as alpha value 
     // This is less transparent 
     p2.setLayout(null); 
     p2.setBackground(new Color(0,0,0,150)); 
     // Sets a border to the JPanel 
     p2.setBorder(new LineBorder(Color.WHITE)); 
     // Sets some size to the panels 
     p2.setBounds(20, 20, 200, 150); 

     // Adds the loading gif 
     String loadLink = "http://i.imgur.com/mHm6LYH.gif"; 
     URL ajaxLoad = null; 
     try { 
      ajaxLoad = new URL(loadLink); 
     } catch (MalformedURLException e3) { 
      // TODO Auto-generated catch block 
      e3.printStackTrace(); 
     } 
     ImageIcon loading = new ImageIcon(ajaxLoad); 
     JLabel loadBar = new JLabel(loading); 
     loadBar.setBounds(70, 60, 54, 55); 
     loadBar.setOpaque(false); 
     p2.add(loadBar); 

即其無需JLabel中的第一圖像中示出的的JPanel。我不能在代碼中顯示JFrame部分,因爲它遍佈整個班級。但我不認爲問題出在JFrame上,所以可能是這個JPanel:/

+0

它的工作完美適合我,根據您的代碼。你可以試試這個代碼,你沒有任何背景圖像共享。 – Braj

+0

對我來說也很類似的代碼:[GifTest](http://luniks.net/other/GifTest.png) –

+0

請問你們可以試一下我在原帖中發佈的編輯代碼嗎?謝謝! – user3508709

回答

3

你的問題是在這裏...

p2.setBackground(new Color(0,0,0,150)); 

搖擺不支持基於阿爾法backgounds,無論是你的組件是透明的或不是。

這樣做意味着組件「嘗試」來使用Alpha值作爲背景填充顏色,但油漆的經理不知道它應該繪製組件的下方,造成的種種問題和問題

現在,這有點棘手。您需要使用setOpaque(false)來使容器透明,但現在這意味着背景未被繪製。

您需要做的是創建一個自定義組件,將它的opaque屬性設置爲false並覆蓋它的paintComponent方法並使用基於alpha的顏色填充背景。我通常喜歡使用AlphaComposite,但這種工作方式逢...

Spinny

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.GridBagLayout; 
import java.awt.HeadlessException; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.EmptyBorder; 

public class TranslucentPanelExample { 

    public static void main(String[] args) { 
     new TranslucentPanelExample(); 
    } 

    public TranslucentPanelExample() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       try { 
        JLabel background = new JLabel(
          new ImageIcon(ImageIO.read(
              getClass().getResource("/background.jpg")))); 
        background.setLayout(new GridBagLayout()); 
        background.add(new WaitPane()); 

        JFrame frame = new JFrame("Testing"); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.add(background); 
        frame.pack(); 
        frame.setLocationRelativeTo(null); 
        frame.setVisible(true); 
       } catch (IOException exp) { 
        exp.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public class WaitPane extends JPanel { 

     public WaitPane() { 
      setLayout(new GridBagLayout()); 
      setBorder(new EmptyBorder(12, 12, 12, 12)); 
      // This is very important 
      setOpaque(false); 
      setBackground(new Color(0, 0, 0, 150)); 

      String loadLink = "http://i.imgur.com/mHm6LYH.gif"; 
      URL ajaxLoad = null; 
      try { 
       ajaxLoad = new URL(loadLink); 
      } catch (MalformedURLException e3) { 
       // TODO Auto-generated catch block 
       e3.printStackTrace(); 
      } 

      ImageIcon loading = new ImageIcon(ajaxLoad); 
      JLabel loadBar = new JLabel(loading); 
      loadBar.setHorizontalAlignment(JLabel.CENTER); 
      loadBar.setVerticalAlignment(JLabel.CENTER); 
      add(loadBar); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.setColor(getBackground()); 
      g.fillRect(0, 0, getWidth(), getHeight()); 
     } 

    } 

} 

佈局管理器,佈局管理器,佈局管理器...

我不能強調不夠多麼重要佈局經理們。你正在依靠的「魔法」數字,這可能並不總是符合現實...

+0

@MadProgammer,非常感謝!我將開始使用佈局管理器。 – user3508709

0

看着你的代碼,我沒有看到任何錯誤。可以肯定的是,我測試了你的代碼,並添加了我自己的JFrame和JPanel。以下是我最終得到的結果:

import javax.swing.*; 
import java.net.*; 

public class Test { 
public static void main(String[] args) { 
    JFrame frame = new JFrame("Test"); 
    frame.setSize(600, 400); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

    JPanel panel = new JPanel(); 
    String loadLink = "http://i.imgur.com/mHm6LYH.gif"; 
    URL ajaxLoad = null; 
    try { 
     ajaxLoad = new URL(loadLink); 
    } catch (MalformedURLException e3) { 
     e3.printStackTrace(); 
    } 
    ImageIcon loading = new ImageIcon(ajaxLoad); 
    JLabel loadBar = new JLabel(loading); 
    loadBar.setBounds(70, 60, 54, 55); 
    loadBar.setOpaque(false); 
    panel.add(loadBar);  
    frame.add(panel); 
    frame.setVisible(true); 
    } 
} 

當我運行它時,它正常工作,沒有任何問題。 .GIF是動畫的,並在框架中的適當位置。你可以複製上面的代碼,並以相同的方式測試它對你的作用嗎?另外,您能否向我們展示您的代碼,在其中初始化包含該圖像的JFrame和JPanel?謝謝。

+0

你可以試一下我在原帖中發佈的編輯代碼嗎?謝謝! – user3508709

0

按照您的第一個快照加載圖像需要時間。請致電Monitoring with an ImageObserver查看圖像的狀態。

將完整加載後的圖像添加到JPanel中。


示例代碼:

new Thread(new Runnable() { 

     @Override 
     public void run() { 
      int width = loading.getIconWidth(); 

      if (width >= 0) { 
       isImageLoaded = true; 
       p2.add(loadBar); 
       return; 
      } 

      // Wait if the image is not loaded yet 
      while (!isImageLoaded) { 
       try { 
        Thread.sleep(500); 
       } catch (InterruptedException ie) { 
       } 
      } 
     } 
    }).start(); 
+0

不幸的是它沒有工作。它仍然和以前一樣,但是當您使用原始文章中發佈的JPanel代碼時,請檢查您是否像我一樣獲得相同的問題。謝謝。 – user3508709

+0

現在,它已經從@MadProgrammer的新代碼中清楚地得到了答案。 – Braj