Swing:如何在每个组件、JPanel、JButton 等上绘制动画?

Swing: how to paint an animation over every component, JPanel, JButton, etc?

我试图在我的 Swing 应用程序中绘制每个组件。

我有:

jButton b = new JButton();
b.addActionListener(e -> fillEntireScreen())
f.add(b);
f.setVisible()...f.setSize()...f.setDefaultCloseOperation()...

其中 fillEntireScreen() 只是一个在屏幕上展开的动画。我覆盖 Component's paintComponent(),在框架上绘画,而不是面板或任何东西。 fileEntireScreen() 在没有按钮的情况下工作,但是当我添加按钮时不起作用,因为动画在按钮后面。

如何在按钮上绘制 谢谢!

改用 glassPane - 示例参见 How to Use Root Panes for more details and How can I paint in an specific JPanel when more than one in same frame- Java

您也可以使用 JLayer,但更复杂,可能不适合您的需要。参见 How to Decorate Components with the JLayer Class