2011-12-03 34 views
3

我在JFrame中有一個半透明背景的JLabel,但是我在文字周圍出現了一些文物。透明背景上的文物

Screenshot of the Artifacts

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.io.IOException; 

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

public class Main { 
    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     JLabel label = new JLabel("Hello World!"); 
     frame.setPreferredSize(new Dimension(200, 200)); 
     frame.setUndecorated(true); 
     frame.setBackground(new Color(128, 128, 128, 128)); 
     //label.setOpaque(false); 
     //label.setBackground(new Color(0, 0, 0, 0)); 
     //((JPanel) frame.getContentPane()).setOpaque(false); 
     //((JPanel) frame.getContentPane()).setBackground(new Color(0, 0, 0, 0)); 
     frame.add(label); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

我已經嘗試過沒有運氣將不透明度爲這些組件。我希望所有的組件都是完全不透明的,所以JFrame的java7每像素透明度似乎是唯一的解決方案。

回答

6

您不能僅將具有透明度的顏色用作背景。請參閱Background With Transparency以獲得解釋和潛在解決方案。

+0

這篇文章指的是背景顏色被重新應用,所以變得越來越不透明,但不處理字母周圍的文物。 – NCode

+0

@NCode該過帳處理具有不透明屬性的組件的責任,以保證組件的背景被完全繪製。當您在不透明組件上使用透明顏色時,可能會出現繪畫問題。這篇文章給出了一個可能發生的事情的例子。它不限制繪畫問題。 – camickr

2

我不能轉載您的問題,也許我是外接電池,但不是有你的GPU的一些問題?

enter image description hereenter image description here

我試圖@camickr建議的基礎上,從教學代碼無可厚非發生

enter image description here

enter image description here

How to Create Translucent and Shaped Windows

import java.awt.*; 
import javax.swing.*; 

public class TranslucentWindow extends JFrame { 

    private static final long serialVersionUID = 1L; 

    public TranslucentWindow() { 
     super("Test translucent window"); 
     this.setLayout(new FlowLayout()); 
     this.add(new JButton("test")); 
     this.add(new JCheckBox("test")); 
     this.add(new JRadioButton("test")); 
     this.add(new JProgressBar(0, 100)); 
     JPanel panel = new JPanel() { 

      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(400, 300); 
      } 
      private static final long serialVersionUID = 1L; 

      @Override 
      protected void paintComponent(Graphics g) { 
       super.paintComponent(g); 
       g.setColor(Color.red); 
       g.fillRect(0, 0, getWidth(), getHeight()); 
      } 
     }; 
     panel.add(new JLabel("Very long textxxxxxxxxxxxxxxxxxxxxx ")); 
     this.add(panel); 
     this.setSize(new Dimension(400, 300)); 
     this.setLocationRelativeTo(null); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

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

      @Override 
      public void run() { 
       Window w = new TranslucentWindow(); 
       w.setVisible(true); 
       com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.7f); 
      } 
     }); 
    } 
} 
+0

有兩個問題:1.您正在使用舊的AWTUtilities,它在Java 7中無法使用,所以您應該使用Window.setOpacity(...),但是這兩個函數都設置了全局透明度,我只需要背景爲了變得透明,組件應該是完全不透明的。 – NCode