簡體   English   中英

使用frame.setBackground(0,255,0,0)在Linux下不起作用

[英]using frame.setBackground(0, 255, 0, 0) doesn't work with linux

所以我通過將背景alpha設置為0使JFrame背景透明,但是如果我在linux中運行該程序,JFrame背景是白色的,這是為什么呢?

好的,我發現它與將圖形渲染到框架有關,這種方法已經過時了,我不再需要在程序中使用它,但是我仍然想知道為什么會發生這種情況(僅在Linux中可以在Windows中找到)

這是一個可運行的例子

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

讓我們從...開始

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

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM