透明背景上的工艺品

3
我在JFrame中使用了一个带有半透明背景的JLabel,但是在字母周围出现了一些伪像。
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个回答

6

单纯使用透明度的颜色作为背景是不行的。请参考透明的背景,了解其中的原因和可能的解决方案。


这篇文章提到背景颜色被重新应用,因此变得越来越不透明,但没有涉及字母周围的伪影问题。 - NCode
@NCode 这篇文章涉及到一个具有不透明属性的组件的责任,即保证组件的背景完全绘制。当您在不透明组件上使用透明颜色时,可能会出现绘制问题。该文章提供了一个“示例”,说明可能发生的情况。它并不限制绘制问题可能出现的形式。 - camickr

2
我无法重现你的问题,可能是我没电了,但是你的GPU有问题吗?
我尝试了@camickr的建议,没有出现任何问题。
基于教程中的代码How to Create Translucent and Shaped Windows。以下是截图: enter image description here enter image description here
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);
            }
        });
    }
}

有两个问题:1. 您正在使用旧的AWTUtilities,它在Java 7中变得无法使用,因此您应该使用Window.setOpacity(...),但是2. 两个函数都设置全局透明度,我只想让背景变透明,组件应该完全不透明。 - NCode

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接