ubuntu上的java jframe透明背景

时间:2014-04-14 18:19:31

标签: java swing ubuntu jframe

我在Ubuntu上运行时编写的代码有问题。我想创建一个透明的JFrame并添加一个图像作为边框。当我在Windows上运行程序工作正常时,但是当我在ubuntu上运行它时,不仅JFrame而且图像也是透明的。

我希望这个程序可以在两个操作系统中工作。我试过这个代码setOpacity(0.0f);太。但是战胜了同样的事情。 请帮助我T_T

很抱歉,但我没有足够的声誉发布图片,所以我把它们作为链接...

这就是我在Ubuntu上看到的:http://oi57.tinypic.com/wgymag.jpg

这是在Windows上:http://oi60.tinypic.com/5o5if4.jpg

这是frameImage的链接:oi58.tinypic.com/2j2xrwy.jpg

以下是我使用的两个类:

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

这是带有背景图片的课程:

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

1 个答案:

答案 0 :(得分:0)