Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用frame.setBackground(0,255,0,0)不会';我不能使用linux_Java_Linux_Swing_Transparency - Fatal编程技术网

Java 使用frame.setBackground(0,255,0,0)不会';我不能使用linux

Java 使用frame.setBackground(0,255,0,0)不会';我不能使用linux,java,linux,swing,transparency,Java,Linux,Swing,Transparency,因此,我已经通过将背景alpha设置为0来使JFrame背景透明,但是如果我在linux中运行该程序,JFrame背景是白色的,这是为什么 好的,我发现它与将图形渲染到帧有关,这个方法很旧,我在程序中不再需要它,但我仍然想知道为什么会发生这种情况(它只是在Linux works中,在windows中查找) 下面是一个可运行的示例 import java.awt.*; import java.awt.Color; import javax.swing.*; class Main extends

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

好的,我发现它与将图形渲染到帧有关,这个方法很旧,我在程序中不再需要它,但我仍然想知道为什么会发生这种情况(它只是在Linux works中,在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);
        }

    }

}


查看并确保您正在测试,看看功能是否可用。经过一点测试后,确定它与挫折方法无关,因此我根本不确定它是什么,因此我还没有更多的信息可以提供。如果有人知道导致此情况的真正原因,这将非常有用,感谢考虑者提供这说明了你的问题。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将导致更少的混乱和更好的响应虽然这将花费我一段时间来编写一个足够小的代码,仍然会产生问题,但我在itok上我已经编辑了我的原始帖子,我不知道这是否会通知你,所以我留下一条评论