【问题标题】:using frame.setBackground(0, 255, 0, 0) doesn't work with linux使用 frame.setBackground(0, 255, 0, 0) 不适用于 linux
【发布时间】:2015-03-13 06:50:34
【问题描述】:

所以我通过将背景 alpha 设置为 0 来使 JFrame 背景透明,但是如果我在 linux 中运行程序,JFrame 背景是白色的,这是为什么?

好的,我发现它与将图形渲染到框架有关,这种方法很旧,我不再需要在程序中使用它,但我仍然想知道为什么会发生这种情况(它只在 Linux 中可以找到窗户)

这是一个可运行的示例

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

class Main extends JFrame{
private void init() {

    this.setPreferredSize(new Dimension(1420, 820));
    this.setUndecorated(true);
    this.setResizable(false);
    this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    this.setBackground(new Color(0, 255, 0, 0));
    this.requestFocus();
    this.setVisible(true);
    this.validate();
    this.pack();
    Color color = UIManager.getColor("activeCaptionBorder");
    this.getRootPane().setBorder(BorderFactory.createLineBorder(color, 1));
    paintInfo();
}

private void paintInfo() {
    Graphics g = this.getGraphics();
    g.setColor(new Color(222, 222, 222, 4));
    g.setFont(new Font("Arial Black", Font.BOLD, 15));
    g.setColor(Color.BLACK);
    g.drawString("test String ",this.getWidth()/2, this.getHeight()/2);
    g.dispose();
}

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    new Main().init();
}
}

【问题讨论】:

  • 查看Determining a Platform's Capabilities 并确保您正在测试该功能是否可用
  • 好的,经过一番测试后,它与 setBackground 方法无关,所以我完全不确定它是什么,所以到目前为止我还没有更多信息可以提供任何人都知道实际导致此问题的原因,这会有所帮助,谢谢
  • 考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将减少混乱并获得更好的响应
  • 是的,我需要一段时间来编写一个足够小的代码,但它仍然会产生问题,但我正在处理它
  • 好的,我已经编辑了我的原始帖子,我不知道这是否会通知您,所以我要发表评论

标签: java linux swing transparency


【解决方案1】:

让我们开始吧……

private void paintInfo() {
    Graphics g = this.getGraphics();
    g.setColor(new Color(222, 222, 222, 4));
    g.setFont(new Font("Arial Black", Font.BOLD, 15));
    g.setColor(Color.BLACK);
    g.drawString("test String ",this.getWidth()/2, this.getHeight()/2);
    g.dispose();
}

Swing 中如何进行绘画并处理您没有创建的Graphics 上下文会导致一些奇怪的事情......

相反,创建您的框架,设置它的透明度,然后向其中添加另一个组件,它会执行您想要的实际绘画,例如...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.LinearGradientPaint;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author swhitehead
 */
public class JavaApplication233 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        GraphicsEnvironment ge
                        = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();

        //If translucent windows aren't supported, exit.
        if (!gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
            System.err.println("Per-pixel translucency is not supported");
            System.exit(0);
        } else {
            new JavaApplication233();
        }
    }

    public JavaApplication233() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setUndecorated(true);
                frame.setBackground(new Color(0, 0, 0, 0));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setOpaque(false);
            setLayout(new GridBagLayout());
            add(new JLabel("I'm not wearing anything"));
//          Color color = UIManager.getColor("activeCaptionBorder");
            setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            LinearGradientPaint lgp = new LinearGradientPaint(
                            new Point(0, 0),
                            new Point(0, getHeight()),
                            new float[]{0f, 1f},
                            new Color[]{applyAlpha(Color.RED), applyAlpha(Color.YELLOW)}
            );
            g2d.setPaint(lgp);
            g2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
            g2d.dispose();
        }

        protected Color applyAlpha(Color color) {
            return new Color(color.getRed(), color.getGreen(), color.getBlue(), 64);
        }

    }

}

【讨论】:

    猜你喜欢
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 2011-07-09
    • 2020-09-06
    • 1970-01-01
    相关资源
    最近更新 更多