【问题标题】:Translucent internal frames - is that possible in java?半透明的内部框架 - 在 java 中可能吗?
【发布时间】:2015-07-17 11:55:06
【问题描述】:

有什么办法可以让半透明的 JInternalFrame 使用摇摆?

我发现的只是 JFrame 的选项(不是内部选项),这不是我需要的。

【问题讨论】:

  • 是的,这很痛苦。你想让所有的框架和它的内容都是透明的吗?
  • @MadProgrammer 是的,因为我需要为所有内容的框架实现淡入效果

标签: java swing jinternalframe translucency


【解决方案1】:

您应该可以使用JLayer。您可以从布局开始,绘制一个与桌面窗格背景颜色相同的完全不透明图层。然后更改 alpha 值以接近单元格,直到完全透明为止。

请参阅 How to Decorate Components With the JLayer Class 上的 Swing 教程部分,了解更多信息和使用透明度进行绘画的示例。

如果您只关心背景,则可以在使用具有透明度的组件时使用Alpha Container

JPanel content = new JPanel( new BorderLayout() );
content.setBackground( new Color(0, 0, 0, 0) );
internalFrame.setContentPane(new AlphaContainer(content));
internalFrame.setOpaque(false);

然后,您可以将内容面板的 alpha 值更改为所需的透明度。

编辑:

这里可能会尝试为内部框架及其组件的透明度设置动画:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class TransparentInternalFrame extends JInternalFrame implements ActionListener
{
    static int openFrameCount = 0;
    static final int xOffset = 30, yOffset = 30;
    private float alpha = 0.0f;
    private Timer timer = new Timer(500, this);

    public TransparentInternalFrame()
    {
        super("Document #" + (++openFrameCount), true, true, true, true);
        setSize(300,300);
        setLocation(xOffset * openFrameCount, yOffset * openFrameCount);
        setVisible( true );
    }

    @Override
    public void paint(Graphics g)
    {
        g.setColor( getDesktopPane().getBackground() );
        g.fillRect(0, 0, getWidth(), getHeight());

        Graphics2D g2 = (Graphics2D)g.create();
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
        super.paint(g2);
    }

    public void showFrame()
    {
        timer.start();
    }

    public void hideFrame()
    {
        alpha = 0.0f;
        repaint();
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        alpha += .05f;
        alpha = Math.min(1.0f, alpha);
        System.out.println(alpha);

        if (alpha >= 1.0f)
            timer.stop();

        repaint();
    }

    private static void createAndShowGUI()
    {
        JDesktopPane desktop = new JDesktopPane();
        desktop.setBackground( Color.YELLOW );

        TransparentInternalFrame tif = new TransparentInternalFrame();
        desktop.add( tif );
        tif.add(new JButton("Hello"), BorderLayout.PAGE_START);

        JButton show = new JButton( "Show Internal Frame" );
        show.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                tif.showFrame();
            }
        });

        JButton hide = new JButton( "Hide Internal Frame" );
        hide.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                tif.hideFrame();
            }
        });

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(show, BorderLayout.PAGE_START);
        frame.add(desktop);
        frame.add(hide, BorderLayout.PAGE_END);
        frame.setLocationByPlatform( true );
        frame.setSize(500, 500);
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

【讨论】:

  • 找不到任何关于将 JLayer 用于真实组件的信息,而不是一些演示面板。您能否举一个正确覆盖 LayerUI 的绘制方法的示例,该方法为图层组件的子对象启用半透明?
  • @maxpovver,它不会使所有组件都半透明。正如我建议的那样,它只会使顶层缓慢半透明,因此看起来每个组件都在慢慢变得更加不透明。这是我能建议的最好的。
猜你喜欢
  • 1970-01-01
  • 2014-01-08
  • 2012-09-13
  • 1970-01-01
  • 2011-11-06
  • 2013-12-29
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多