简体   繁体   English

未修饰的 JFrame 阴影

[英]Undecorated JFrame shadow

How do you add a shadow to a undecorated jframe?如何为未修饰的 jframe 添加阴影?

From what I found online, you might be able to add the jframe to another black translucent window to give a shadow effect.根据我在网上找到的内容,您可以将 jframe 添加到另一个黑色半透明窗口以提供阴影效果。 Or somehow apply something like this to a JFrame:或者以某种方式将这样的东西应用于 JFrame:

    Border loweredBorder = new EtchedBorder(EtchedBorder.LOWERED);
    setBorder(loweredBorder);

Either way I just want to know the best method or maybe a completely different way of getting the same effect like extending from another class and not jframe.无论哪种方式,我只想知道最好的方法,或者可能是一种完全不同的方式来获得相同的效果,例如从另一个类而不是 jframe 扩展。 I'm new to Java so I might be going down the wrong direction so any advice is appreciated.我是 Java 新手,所以我可能会走错方向,所以任何建议都值得赞赏。

Basically, you need to make a series of layers.基本上,您需要制作一系列图层。

  • JFrame
  • ShadowPanel
  • and content...和内容...

在此处输入图片说明

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class ShadowWindow {

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

    public ShadowWindow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setUndecorated(true);
                frame.setBackground(new Color(0, 0, 0, 0));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new ShadowPane());

                JPanel panel = new JPanel(new GridBagLayout());
                panel.add(new JLabel("Look ma, no hands"));

                frame.add(panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ShadowPane extends JPanel {

        public ShadowPane() {
            setLayout(new BorderLayout());
            setOpaque(false);
            setBackground(Color.BLACK);
            setBorder(new EmptyBorder(0, 0, 10, 10));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
            g2d.fillRect(10, 10, getWidth(), getHeight());
            g2d.dispose();
        }
    }
}

MadProgammer,疯狂程序员,

frame.setBackground(new Color(0, 0, 0, 0));

is useless if you don't use also:如果你不使用也是没用的:

frame.setOpacity(1.0f);

Setting the default close operation to EXIT_ON_CLSE is bad, the app should close naturely with the end of both processes, so DISPOSE_ON_CLOSE is the right way, reserving the other one to "fix" bugs.将默认关闭操作设置为 EXIT_ON_CLSE 是不好的,应用程序应该在两个进程结束时自然关闭,所以 DISPOSE_ON_CLOSE 是正确的方法,保留另一个来“修复”错误。

frame.setContentPane(new ShadowPanel());

is useless if you use also (and after):如果您还使用(以及之后),则无用:

frame.add(panel);

ShadowPanel's constructor should start with: ShadowPanel 的构造函数应该以:

super(new GridBagLayout());

instead of:而不是:

setLayout(new GridBagLayout());

Additionnally, using a GridBagLayout to only add a single component is a bit disproportionned, isn't it ?另外,使用 GridBagLayout 仅添加单个组件有点不成比例,不是吗? What about a GridLayout(1, 1) or even a lazy BorderLayout ? GridLayout(1, 1) 甚至懒惰的 BorderLayout 怎么样?

Return a new Dimension on each call on the preferred size (many of them are done by the machine, during a lot of operation, like repainting, etc) is very expensive.在每次调用首选大小时返回一个新维度(其中许多是由机器完成的,在大量操作期间,例如重新绘制等)非常昂贵。 You should return a variable defined in the constructor or the class.您应该返回在构造函数或类中定义的变量。

Why override getPreferredSize(), but not getBackground(), nor isOpaque() ?为什么要覆盖 getPreferredSize(),而不是 getBackground() 和 isOpaque() ?

GraphicsD should be placed inside a try block to dispose of it inside a finally block. GraphicsD 应该放在 try 块内,以便在 finally 块内处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM