透明背景上的文物

发布于 2024-12-19 04:40:20 字数 1061 浏览 4 评论 0原文

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

I've got a JLabel in a JFrame with a semi-transparent background, but I'm getting some artifacts around the letters.

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);
    }
}

I already tried applying opacity to these components without luck. I'd like all Components to be fully opaque, so java7 per-pixel transparency for the JFrame seems to be the only solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那支青花 2024-12-26 04:40:20

您不能只使用具有透明度的颜色作为背景。请参阅透明背景了解说明和潜在的解决方案。

You can't just use Colors with transparency as a background. See Background With Transparency for an explanation and potential solution.

与酒说心事 2024-12-26 04:40:20

我无法重现您的问题,也许我的电池没电了,但是您的 GPU 没有问题吗???

在此处输入图像描述 在此处输入图像描述

我尝试了 @camickr 的建议,没有发生任何错误

在此处输入图像描述

在此处输入图像描述

基于教程 如何创建半透明和形状的窗口

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);
            }
        });
    }
}

I can't to reproduced your issue, maybe I'm out-off battery, but isn't there some issue with your GPU ???

enter image description here enter image description here

I tried suggestion by @camickr, nothing wrong happened

enter image description here

and

enter image description here

based on code from tutorial 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);
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文