在透明背景上的文物

时间:2011-12-03 23:45:19

标签: java swing transparency jframe

我在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每像素透明度似乎是唯一的解决方案。

2 个答案:

答案 0 :(得分:6)

您不能只使用透明度为背景的颜色。有关解释和可能的解决方案,请参阅Background With Transparency

答案 1 :(得分:2)

我不能再现你的问题,也许我的电池没电了,但你的GPU没有问题???

enter image description here enter 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);
            }
        });
    }
}