简体   繁体   English

在Ubuntu上的Java JFrame透明背景

[英]java jframe transparent background on ubuntu

I have a problem with the code that I wrote when I run it on Ubuntu. 我在Ubuntu上运行时编写的代码有问题。 I want to create a transparent JFrame and add an image as a border. 我想创建一个透明的JFrame并将图像添加为边框。 When I run the program on Windows is working properly, but when I run it on ubuntu not only the JFrame but also the image is transparent. 当我在Windows上运行该程序时,它可以正常工作,但是当我在ubuntu上运行该程序时,不仅JFrame而且图像都是透明的。

I'd like the program to work in both os. 我希望该程序在两个操作系统中都能工作。 I tried with this code setOpacity(0.0f); 我尝试使用此代码setOpacity(0.0f); too. 太。 But the outcame is the same. 但是结果是一样的。 Help me please T_T 请帮我T_T

Sorry but I don't have enough reputation to post images so I put them as links... 抱歉,但是我没有足够的声誉来发布图像,因此我将其作为链接...

This is what i see on Ubuntu: http://oi57.tinypic.com/wgymag.jpg 这是我在Ubuntu上看到的: http : //oi57.tinypic.com/wgymag.jpg

and this is on Windows: http://oi60.tinypic.com/5o5if4.jpg 这是在Windows上: http : //oi60.tinypic.com/5o5if4.jpg

and this is the link to the frameImage: oi58.tinypic.com/2j2xrwy.jpg 这是指向frameImage的链接:oi58.tinypic.com/2j2xrwy.jpg

Here are the two classes that I used: 这是我使用的两个类:

MainClass: 主类:

import com.sun.awt.AWTUtilities;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;



public class Window extends JFrame {


    private final int width, height;

    private ContainerPanel container;

    public Window() {

        this.width = 1024;

        this.height = 688;

        this.setSize(width, height);

        this.setMaximumSize(new Dimension(width, height));

        this.setMinimumSize(new Dimension(width, height));

        this.setResizable(false);

        this.setUndecorated(true);

        this.setBackground(new Color(0, 0, 0, 0));

        container = new ContainerPanel(Window.this, 1024, 688);

        this.setContentPane(container);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                try {
                } catch (Exception e1) {
                    System.exit(0);
                    e1.printStackTrace();
                }
            }
        });

        this.setVisible(true);

        this.pack();

    }

    public static void main(String[] args) {
        System.out.println("TRANSLUCENT supported:          " + AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT));
        System.out.println("PERPIXEL_TRANSPARENT supported: " + AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.PERPIXEL_TRANSPARENT));
        System.out.println("PERPIXEL_TRANSLUCENT supported: " + AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.PERPIXEL_TRANSLUCENT));
        new Window();
    }
}

and this is the class with the background image: 这是带有背景图片的类:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class ContainerPanel extends JPanel {


    private final Window window;

    private JLabel label;

    private Point mouseDownCompCoords, currentLocationOnScreen;

    private final int windowWidth, windowHeight;

    private int closeButtonPosX, closeButtonPosY;

    private BufferedImage frameImg;


    public ContainerPanel(Window window, int windowWidth, int windowHeight) {
        this.window = window;
        this.windowWidth = windowWidth;
        this.windowHeight = windowHeight;
        this.closeButtonPosX = 900;
        this.closeButtonPosY = 19;
        this.setLayout(null);
        this.setOpaque(false);
        this.setSize(this.windowWidth, this.windowHeight);
        this.setMaximumSize(new Dimension(this.windowWidth, this.windowHeight));
        this.setMinimumSize(new Dimension(this.windowWidth, this.windowHeight));
        crateLabel();
        try {
            createButton();
            frameImg = ImageIO.read(new File("Images/Frame/frame.png"));
        } catch (IOException ex) {
            Logger.getLogger(ContainerPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setVisible(true);
    }


    private void crateLabel() {
        label = new JLabel();
        final int labelHeight = 45;
        label.setSize(windowWidth, labelHeight);
        label.setMaximumSize(new Dimension(windowWidth, labelHeight));
        label.setMinimumSize(new Dimension(windowWidth, labelHeight));
        label.setLocation(0, 0);
        label.setOpaque(false);
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                mouseDownCompCoords = e.getPoint(); 
            }
        });

        label.addMouseMotionListener(new MouseMotionListener() {

            @Override
            public void mouseDragged(MouseEvent e) {
                currentLocationOnScreen = e.getLocationOnScreen();
                ContainerPanel.this.window.setLocation(currentLocationOnScreen.x - mouseDownCompCoords.x, currentLocationOnScreen.y - mouseDownCompCoords.y);
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                //do nothing
            }
        });
        label.setVisible(true);
        this.add(label);
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        super.paintComponent(g2d);
       g2d.drawImage(frameImg, 0, 0, windowWidth, windowHeight, null);   
    }



    private void createButton() {
        JButton close = new JButton("close");
        close.setLocation(this.closeButtonPosX , this.closeButtonPosY);
        close.setSize(100, 30);
        close.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                System.exit(0);
            }
        });
        label.add(close);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM