Translucent JPopupMenu inside a Translucent Window - alternative?

后端 未结 1 571
死守一世寂寞
死守一世寂寞 2020-12-21 02:30

I\'m not sure if this is possible, but is there a way to safely allow popups to be translucent even when the parent container is also translucent?

If not,

相关标签:
1条回答
  • 2020-12-21 02:55

    Try this code part, I had used JWindow though

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class WindowExample
    {
        private JWindow window;
        private JLabel updateLabel;
        private int count = 5;
        private Timer timer;
        private int x;
        private int y;
        private ActionListener timerAction = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                updateLabel.setText("Closing Window in " + count + " seconds...");
                count--;
                if (count == 0)
                {
                    timer.stop();
                    window.setVisible(false);
                    window.dispose();
                }   
            }
        };
    
        private void createAndDisplayGUI()
        {
            final JFrame frame = new JFrame("Window Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.setUndecorated(true);
            frame.setOpacity(0.5f);
            frame.addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent me)
                {
                    x = me.getX();
                    y = me.getY();
                    window = new JWindow();
                    JPanel contentPane = new JPanel();
                    JLabel positionLabel = new JLabel(
                        "X : " + me.getX() + " Y : " + me.getY());
                    updateLabel = new JLabel("TImer");  
                    contentPane.setLayout(new BorderLayout(5, 5));
                    contentPane.add(updateLabel, BorderLayout.CENTER);
                    contentPane.add(positionLabel, BorderLayout.PAGE_END);
                    window.setContentPane(contentPane);
                    window.setOpacity(0.5f);
                    window.setSize(200, 100);
                    window.setLocation(x + window.getWidth(), y + window.getHeight());
                    window.setVisible(true);
                    count = 5;
                    timer = new Timer(1000, timerAction);
                    timer.start();
                }
            });
    
            frame.setSize(500, 500);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new WindowExample().createAndDisplayGUI();
                }
            });
        }
    }
    

    And here is the output :

    TRANSLUCENT

    WARNING I AM GETTING

    C:\Mine\JAVA\J2SE>javac -d classes src\OpaqueWindowSSCCE.java
    src\OpaqueWindowSSCCE.java:1: warning: AWTUtilities is internal proprietary API and may be removed i
    n a future release
    import com.sun.awt.AWTUtilities;
                      ^
    src\OpaqueWindowSSCCE.java:68: warning: AWTUtilities is internal proprietary API and may be removed
    in a future release
            AWTUtilities.setWindowOpaque(frame, false);
            ^
    src\OpaqueWindowSSCCE.java:69: warning: AWTUtilities is internal proprietary API and may be removed
    in a future release
            AWTUtilities.setWindowOpaque(window, false);
            ^
    3 warnings
    
    0 讨论(0)
提交回复
热议问题