2014-02-05 61 views
6

嘿,我正在嘗試製作某種啓動器,並且「窗口」必須是透明的,因爲我希望我使用的圖像是它的設計,如果您明白我的意思。我試圖做setUndecorated(true);setBackground(new Color(0, 0, 0, 0));,但它看起來很奇怪。這裏是它的外觀圖片:http://prntscr.com/2pqohq這裏是我的代碼:如何使JFrame背景和JPanel透明,僅顯示圖像

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.GridBagLayout; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

/** 
* 
* Our Launcher for the client 
* 
* @author Daniel <Skype: daniel.gusdal> 
* 
* Current Date: 5. feb. 2014 Current Time: 14:29:54 
* Project: 742 client. File Name: Launcher.java 
* 
*/ 
public class Launcher2 { 

    public Launcher2() { 
     JFrame frame = new JFrame(); 
     frame.getContentPane().add(new ImagePanel()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //frame.pack(); //had to remove, got an error from it... 
     frame.setUndecorated(true); //transparent 
     frame.setBackground(new Color(0, 0, 0, 0)); //transparent 
     frame.setVisible(true); 
     frame.setSize(1080, 550); 
    } 

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

    @SuppressWarnings("serial") 
    public class ImagePanel extends JPanel { 

     BufferedImage img; 

     public ImagePanel() { 
      setLayout(new GridBagLayout()); 
      try { 
       img = ImageIO.read(new File("C:/Users/Daniel/Pictures/Launcher3.png/")); 
      } catch (MalformedURLException ex) { 
       Logger.getLogger(Launcher2.class.getName()).log(Level.SEVERE, null, ex); 
      } catch (IOException ex) { 
       Logger.getLogger(Launcher2.class.getName()).log(Level.SEVERE, null, ex); 
      } 

     } 

     /** 
     * Draws the image and sets the image dimension 
     */ 
     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      //g.drawImage(img, 100, 100, 1080, 550, this); 
      g.drawImage(img, 0, 0, getWidth(), getHeight(), this); 
     } 

     /** 
     * Sets the JPanel dimension 
     */ 
     public Dimension getPreferredSize() { 
      return new Dimension(1080, 550); 
     } 
    } 
} 

回答

7

您需要設置ImagePanelsetOpaque(false)

public ImagePanel() { 
     setOpaque(false); 

你也都拿到了異常,因爲你需要setUndecorate(true)pack();

JFrame frame = new JFrame(); 
frame.add(new ImagePanel()); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setUndecorated(true); 
frame.pack(); 
frame.setBackground(new Color(0, 0, 0, 0)); 
frame.setVisible(true); 

那些AR只有兩件事我改變了,而且很有效。我也擺脫了setSize()

也使用frame.setLocationRelativeTo(null);pack()中心的框架。


這裏有一個例子(只是爲未來的讀者,因爲我覺得這可能是一種流行的問題)

enter image description here

import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.URL; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

public class CircleSplashScreen { 

    public CircleSplashScreen() { 
     JFrame frame = new JFrame(); 
     frame.getContentPane().add(new ImagePanel()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setUndecorated(true); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setBackground(new Color(0, 0, 0, 0)); 
     frame.setVisible(true); 
    } 

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

    @SuppressWarnings("serial") 
    public class ImagePanel extends JPanel { 

     BufferedImage img; 

     public ImagePanel() { 
      setOpaque(false); 
      setLayout(new GridBagLayout()); 
      try { 
       img = ImageIO.read(new URL("http://www.iconsdb.com/icons/preview/royal-blue/stackoverflow-4-xxl.png")); 
      } catch (IOException ex) { 
       Logger.getLogger(CircleSplashScreen.class.getName()).log(Level.SEVERE, null, ex); 
      } 

     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.drawImage(img, 0, 0, getWidth(), getHeight(), this); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(500, 500); 
     } 
    } 
} 
+0

這麼多再次peeskillet謝謝,最後的作品! :)不能接受的答案呢......必須等4分鐘大聲笑 – Daniel

+0

看到我的答案的最底部。我添加了一行。 –

+0

現在是否:D再次感謝!^_ ^ – Daniel