2012-05-02 143 views
4

我想要做的JFrame邊框透明的,所以我想這樣做,用我自己的邊框類...半透明的JFrame邊境

private class ShadowBorder extends AbstractBorder { 

    private static final int RADIUS = 30; 

    @Override 
    public boolean isBorderOpaque() { 
     return false; 
    } 

    @Override 
    public Insets getBorderInsets(Component c) { 
     return new Insets(RADIUS, RADIUS, RADIUS, RADIUS); 
    } 

    @Override 
    public Insets getBorderInsets(Component c, Insets insets) { 
     insets.top = RADIUS; 
     insets.left = RADIUS; 
     insets.bottom = RADIUS; 
     insets.right = RADIUS; 
     return insets; 
    } 

    @Override 
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 
      Graphics2D g2d = (Graphics2D) g.create(); 
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
           RenderingHints.VALUE_ANTIALIAS_ON); 
      g2d.setColor(new Color(0x66000000, true)); 

      g2d.fillRect(0, 0, width - RADIUS, RADIUS); 
     } 
} 

但邊框不是透明的決賽。邊界內部也有白色背景,但我不知道爲什麼(請參閱atach。image)。有任何想法嗎?

enter image description here

謝謝!

回答

4

您需要將窗口設置爲不透明並在圖形上使用複合。此外,在您的代碼中,您只打印一個邊框,而不是四個邊框,這就是爲什麼您只能看到一個邊框的原因。像這樣的東西應該這樣做(儘管它會更好地作畫基礎上,插圖的邊界,而不是你的RADIUS常數):

import java.awt.AlphaComposite; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Insets; 
import java.awt.RenderingHints; 
import java.io.UnsupportedEncodingException; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.AbstractBorder; 

import com.sun.awt.AWTUtilities; 

public class Main { 

    private static class ShadowBorder extends AbstractBorder { 

     private static final int RADIUS = 30; 

     @Override 
     public boolean isBorderOpaque() { 
      return false; 
     } 

     @Override 
     public Insets getBorderInsets(Component c) { 
      return new Insets(RADIUS, RADIUS, RADIUS, RADIUS); 
     } 

     @Override 
     public Insets getBorderInsets(Component c, Insets insets) { 
      insets.top = RADIUS; 
      insets.left = RADIUS; 
      insets.bottom = RADIUS; 
      insets.right = RADIUS; 
      return insets; 
     } 

     @Override 
     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 
      Graphics2D g2d = (Graphics2D) g.create(); 
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
      g2d.setColor(new Color(66, 0, 0)); 
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_ATOP, 0.5f)); 
      g2d.fillRect(0, 0, width - RADIUS, RADIUS); 
      g2d.fillRect(width - RADIUS, 0, RADIUS, height - RADIUS); 
      g2d.fillRect(0, RADIUS, RADIUS, height - RADIUS); 
      g2d.fillRect(RADIUS, height - RADIUS, width - RADIUS, RADIUS); 
     } 
    } 

    public static void main(String[] args) throws UnsupportedEncodingException { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setUndecorated(true); 
     AWTUtilities.setWindowOpaque(frame, false); 
     JPanel panel = new JPanel(new BorderLayout()); 
     JButton button = new JButton("Hello"); 
     panel.add(button); 
     panel.setBorder(new ShadowBorder()); 
     frame.setContentPane(panel); 
     frame.setSize(300, 200); 
     frame.setVisible(true); 
    } 
} 
+0

明白了,非常感謝! –

+0

我已經嘗試過使用JRE 7這種方法,它不起作用(邊框有一些灰色背景)。在JRE 6中,它工作正常,但我需要在JRE 7上運行它。 –

5

要使JFrame的使用,你必須首先改變幀的不透明度定製的透明邊框:

frame.setUndecorated (true); 
AWTUtilities.setWindowOpaque (frame, false); 

之後,你的框架不會有任何系統的邊框和背景可言,所以你可以畫一些透明的(對於例如邊界)。順便說一下,你改變邊界的組件必須是非透明的,以及組件下方的容器,以使框架後面的像素「通過」。

您也可以使整個框架半透明的,有些情況是:

AWTUtilities.setWindowOpacity (frame, 0.5f); 

這將使50%透明(和它的作品,即使系統窗口裝飾)。

儘管這些功能僅在Win和Mac OS系統上得到了正確支持。

+0

這使得整個邊框透明,而不僅僅是邊界。 –

+1

我已經說過 - 「你可以讓***整個框架半透明」。我剛纔提到了這種方法,因爲它在某些情況下可能對問題作者有用,並且與窗口透明度的整個概念類似。 –