一个半透明窗口半透明里面JPopupMenu的 - 另类? [英] Translucent JPopupMenu inside a Translucent Window - alternative?

查看:682
本文介绍了一个半透明窗口半透明里面JPopupMenu的 - 另类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是可能的,但有没有办法安全地允许弹出窗口为半透明,即使父容器也是半透明的?结果

如果不是,什么是明智的替代品使用或扩展而不是的JP​​opupMenu

请注意:半透明的指的是不是有背景,类似于 setOpaque(假)的效果的成分; 。谢谢你。


搜索结果
从2009年的一个论坛,回答用户的 camickr 的:


  

我不知道是否透明画1.6.0_10发生了变化。先
  以我认为透明度只能在轻量级实现
  各个部分(即摇摆完成了所有的画)。 JFrame的,和的JWindow
  的JDialog不是轻量级的,因为他们使用的操作系统组件。


  
  

在弹出的情况下,它完全包含当是轻量级
  其父框架之内。但一个轻量级的弹出不能涂
  该帧的这样一个的JWindow(我相信)用作边界之外
  弹出,这不可能是透明的。


SSCCE:显示半透明的JWindow在半透明JFrame的顶部

 进口com.sun.awt.AWTUtilities;
进口java.awt.Color中;
进口java.awt.Graphics;
进口java.awt.Graphics2D中;
进口java.awt.RenderingHints中;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口的javax.swing *。公共类OpaqueWindowSSCCE {    私人INT倒计时= 5;    公共静态无效的主要(字串[] args){
        新OpaqueWindowSSCCE();
    }    公共OpaqueWindowSSCCE(){
        最终的JFrame帧=新的JFrame(OpaqueWindowSSCCE);
        最终的JWindow窗口=新的JWindow();        新的Timer(1000,新的ActionListener(){            @覆盖
            公共无效的actionPerformed(ActionEvent的五){
                如果( - 倒计时== 0){
                    frame.dispose();
                    window.dispose();
                    System.exit(0);
                }其他{
                    frame.repaint();
                }
            }        })。开始();        frame.setContentPane(新JPanel(){            @覆盖
            公共无效的paintComponent(图形paramGraphics){
                super.paintComponent方法(paramGraphics);
                Graphics2D的G =(Graphics2D的)paramGraphics.create();
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.setColor(新彩(50,50,50));
                g.fillRoundRect(0,0,的getWidth(),的getHeight(),10,10);
                g.setColor(新的色彩(180,180,180));
                g.drawString(+倒数+秒,在关闭,20,25);
            }
        });        window.setContentPane(新JPanel(){            @覆盖
            公共无效的paintComponent(图形paramGraphics){
                super.paintComponent方法(paramGraphics);
                Graphics2D的G =(Graphics2D的)paramGraphics.create();
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.setColor(新的色彩(180,180,180));
                g.fillRoundRect(0,0,的getWidth(),的getHeight(),10,10);
            }
        });        frame.setUndecorated(真);        ((JComponent中)frame.getContentPane())setOpaque(假)。
        ((JComponent中)window.getContentPane())setOpaque(假)。        AWTUtilities.setWindowOpaque(帧,FALSE);
        AWTUtilities.setWindowOpaque(窗口,FALSE);        window.setAlwaysOnTop(真);        frame.setBounds(200200500500);
        window.setBounds(600600200200);
        frame.setVisible(真);
        window.setVisible(真);
    }
}


解决方案

试试这个code部分,我用了的JWindow 虽然

 进口java.awt中的*。
java.awt.event中导入*。
进口的javax.swing *。公共类WindowExample
{
    私人的JWindow窗口;
    私人的JLabel updateLabel;
    私人诠释计数= 5;
    私人定时器定时器;
    私人诠释X;
    私人诠释Ÿ;
    私人的ActionListener timerAction =新的ActionListener()
    {
        公共无效的actionPerformed(ActionEvent的AE)
        {
            updateLabel.setText(+数+,在关闭窗口秒......);
            计数 - ;
            如果(计数== 0)
            {
                timer.stop();
                window.setVisible(假);
                window.dispose();
            }
        }
    };    私人无效createAndDisplayGUI()
    {
        最终的JFrame帧=新的JFrame(窗口示例);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setUndecorated(真);
        frame.setOpacity(0.5F);
        frame.addMouseListener(新MouseAdapter()
        {
            公共无效的mouseClicked(我的MouseEvent)
            {
                X = me.getX();
                Y = me.getY();
                窗口=新的JWindow();
                的JPanel的contentPane =新JPanel();
                JLabel的positionLabel =新的JLabel(
                    X+ me.getX()+Y:+ me.getY());
                updateLabel =新的JLabel(计时器);
                contentPane.setLayout(新的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(真);
                数= 5;
                定时器=新定时器(1000,timerAction);
                timer.start();
            }
        });        frame.setSize(500,500);
        frame.setLocationRelativeTo(NULL);
        frame.setVisible(真);
    }    公共静态无效的主要(字符串参数... args)
    {
        SwingUtilities.invokeLater(Runnable的新()
        {
            公共无效的run()
            {
                新WindowExample()createAndDisplayGUI()。
            }
        });
    }
}

这里是输出:

警告我收到

  C:\\煤矿\\ JAVA \\ J2SE>的javac -d类的src \\ OpaqueWindowSSCCE.java
SRC \\ OpaqueWindowSSCCE.java:1:警告:AWTUtilities是内部的私有API和可能会被删除我
N A未来的版本
进口com.sun.awt.AWTUtilities;
                  ^
SRC \\ OpaqueWindowSSCCE.java:68:警告:AWTUtilities是内部的私有API和可能会被删除
在未来的版本
        AWTUtilities.setWindowOpaque(帧,FALSE);
        ^
SRC \\ OpaqueWindowSSCCE.java:69:警告:AWTUtilities是内部的私有API和可能会被删除
在未来的版本
        AWTUtilities.setWindowOpaque(窗口,FALSE);
        ^
3警告

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, what would be wise alternative to use or extend instead of JPopupMenu?

Note: Translucent refers to a component not 'having a background', similar to the effect of setOpaque(false);. Thanks.



From a forum answer by user camickr in 2009:

I don't know if transparency painting has changed in 1.6.0_10. Prior to that I believe transparency can only be achieved in lightweight components (ie. Swing does all the painting). JFrame, JWindow and JDialog are not lightweight because they use OS components.

In the case of a popup, it is lightweight when entirely contained within its parent frame. But a lightweight popup can not be painted outside the bounds of the frame so a JWindow (I believe) is used as the popup, which can't be transparent.

SSCCE: Showing translucent JWindow over the top of translucent JFrame

import com.sun.awt.AWTUtilities;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class OpaqueWindowSSCCE {

    private int countdown = 5;

    public static void main(String[] args) {
        new OpaqueWindowSSCCE();
    }

    public OpaqueWindowSSCCE() {
        final JFrame frame = new JFrame("OpaqueWindowSSCCE");
        final JWindow window = new JWindow();

        new Timer(1000, new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                if(--countdown == 0){
                    frame.dispose();
                    window.dispose();
                    System.exit(0);
                } else {
                    frame.repaint();
                }
            }

        }).start();

        frame.setContentPane(new JPanel() {

            @Override
            public void paintComponent(Graphics paramGraphics) {
                super.paintComponent(paramGraphics);
                Graphics2D g = (Graphics2D) paramGraphics.create();
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g.setColor(new Color(50, 50, 50));
                g.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
                g.setColor(new Color(180, 180, 180));
                g.drawString("Closing in " + countdown + " seconds", 20, 25);
            }
        });

        window.setContentPane(new JPanel() {

            @Override
            public void paintComponent(Graphics paramGraphics) {
                super.paintComponent(paramGraphics);
                Graphics2D g = (Graphics2D) paramGraphics.create();
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g.setColor(new Color(180, 180, 180));
                g.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
            }
        });

        frame.setUndecorated(true);

        ((JComponent) frame.getContentPane()).setOpaque(false);
        ((JComponent) window.getContentPane()).setOpaque(false);

        AWTUtilities.setWindowOpaque(frame, false);
        AWTUtilities.setWindowOpaque(window, false);

        window.setAlwaysOnTop(true);

        frame.setBounds(200,200,500,500);
        window.setBounds(600,600,200,200);
        frame.setVisible(true);
        window.setVisible(true);
    }
}

解决方案

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 :

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

这篇关于一个半透明窗口半透明里面JPopupMenu的 - 另类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆