[英]Swing - Creating a tinted transparent foreground while making the background items not clickable
So I'm creating a Java Application User Interface using Swing. 因此,我正在使用Swing创建Java应用程序用户界面。 Basically, whenever a process is being completed, I want to have an Overlay that's somewhat transparent with a certain tint, and in the foreground a loading gif would be displayed.
基本上,每当一个过程完成时,我都希望有一个带有某种色彩的透明叠加层,并且在前台会显示一个加载的gif。 The background items will be not clickable to prevent any other processes being loaded/run at once.
后台项目将不可单击,以防止立即加载/运行任何其他进程。 The only part I really need help with is making the tinted background and making the items in the background not clickable.
我真正需要帮助的唯一部分是使有色背景和使背景中的项目不可单击。 Ideally, I want this class to be used as a Util class to be used in other case where I would need to start a loading screen.
理想情况下,我希望将此类用作Util类,以在需要启动加载屏幕的其他情况下使用。 So far, this is my code:
到目前为止,这是我的代码:
package com.cervinakuy.game;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Overlay extends JFrame {
private static final long serialVersionUID = 1L;
public Overlay() {
setUndecorated(true);
setBackground(new Color(1.0f,1.0f,1.0f,0.5f));
setAlwaysOnTop(true);
getRootPane().putClientProperty("apple.awt.draggableWindowBackground", false);
setVisible(true);
pack();
}
}
You can do what you are doing except you would use a modal JDialog
instead of a JFrame. 除了可以使用
modal JDialog
而不是JFrame之外,您可以执行其他操作。 A modal dialog prevents events from being passed to the parent JFrame until the dialog is closed. 模态对话框可防止在对话框关闭之前将事件传递给父JFrame。
Another approach is to use a Glass Pane
. 另一种方法是使用
Glass Pane
。 The glass pane can cover the frame. 玻璃窗格可以覆盖框架。 You can then intercept the Mouse/Key events to prevent further processing until the glass pane is removed.
然后,您可以拦截Mouse / Key事件以防止进一步处理,直到卸下玻璃窗格为止。
Check out Disabled Glass Pane for an example of this approach. 请查看“ 禁用玻璃窗格”以获取此方法的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.