Re-paint on translucent frame/panel/component.

后端 未结 4 1245
一个人的身影
一个人的身影 2020-11-22 15:42

I\'m trying to create a translucent window with Java on OSX and add a JLabel to it.

This JLabel changes its text every second....

4条回答
  •  心在旅途
    2020-11-22 16:07

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package mreg;
    
    import java.awt.AlphaComposite;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JWindow;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    
    /**
     *
     * @author Manoj
     */
    public class TranscluentWindow {
    
     public static void main(String[] args) {
            new TranscluentWindow();
        }
    
        public TranscluentWindow() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        try {
                            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                        } catch (Exception ex) {
                        }
    
                        JWindow frame = new JWindow();
                        frame.setAlwaysOnTop(true);
                        frame.addMouseListener(new MouseAdapter() {
    
    
    
                        });
                        frame.setBackground(new Color(0,0,0,0));
                        frame.setContentPane(new TranslucentPane());
                        frame.add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/124742-high-school-collection/png/image_4.png")))));
                        frame.pack();
                        frame.setLocationRelativeTo(null);
                        frame.setVisible(true);
    
                           new Thread(new Runnable() {
                public void run() {
    
                        try {
    
                            Thread.sleep(2500);
                        } catch (InterruptedException ex) {
                        }
    
                    frame.dispose();
                    new loging().setVisible(true);
                }
            }).start();
    
    
    
    
    
    
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
    
                }
            });
        }
    
        public class TranslucentPane extends JPanel {
    
            public TranslucentPane() {
                setOpaque(false);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g); 
    
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setComposite(AlphaComposite.SrcOver.derive(0.0f));
                g2d.setColor(getBackground());
                g2d.fillRect(0, 0, getWidth(), getHeight());
    
            }
    
        }   
    
    
    
    }
    

提交回复
热议问题