在屏幕上显示消息[英] Display a message on the screen

问题描述

我想在按下按钮时在屏幕上显示一条消息.消息不应有任何窗口,并且应显示在屏幕中央,覆盖任何可能存在的内容.有点像这里.我将如何实现这样的效果?

我已尝试到处搜索,但问题的性质不会产生结果.感谢任何人帮助我指出正确的方向.

更新:对于那些想知道的人.这是我正在做的项目,感谢 MadProgrammer 的帮助.你救了我的眼睛.

推荐答案

我能想到至少三种在 Swing 中做到这一点的方法...

玻璃板

如果您只是想在当前框架上显示内容,那么您可以使用当前框架玻璃窗格...

GlassPane

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class OverlayTest {

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

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

                final OverlayPane overlay = new OverlayPane();
                JButton show = new JButton("Show");
                show.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        overlay.setVisible(true);
                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setGlassPane(overlay);
                frame.setLayout(new GridBagLayout());
                frame.add(show);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class OverlayPane extends JPanel {

        private JLabel label;

        public OverlayPane() {
            setLayout(new GridBagLayout());
            label = new JLabel("1");
            label.setFont(label.getFont().deriveFont(Font.BOLD, 96f));
            add(label);
            setOpaque(false);
        }

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

    }

}

了解如何使用如何使用根窗格

JLayedPane

如果您想在当前框架内的特定组件上显示内容,那么您可以利用 JLayeredPane,它的作用很像组件的玻璃窗格...

请参阅如何使用 JLayer 类装饰组件了解更多

未装饰的框架

透明框架

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class OverlayTest2 {

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

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

                final JFrame masterFrame = new JFrame("Testing");

                final OverlayPane overlay = new OverlayPane();
                JButton show = new JButton("Show");
                show.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                        JFrame frame = new JFrame();
                        frame.setUndecorated(true);
                        frame.setBackground(new Color(0, 0, 0, 0));
                        frame.add(new OverlayPane());
                        frame.pack();
                        frame.setLocationRelativeTo(masterFrame);
                        frame.setVisible(true);

                    }
                });

                masterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                masterFrame.setGlassPane(overlay);
                masterFrame.setLayout(new GridBagLayout());
                masterFrame.add(show);
                masterFrame.pack();
                masterFrame.setLocationRelativeTo(null);
                masterFrame.setVisible(true);
            }
        });
    }

    public class OverlayPane extends JPanel {

        private JLabel label;

        public OverlayPane() {
            setLayout(new GridBagLayout());
            label = new JLabel("1");
            label.setFont(label.getFont().deriveFont(Font.BOLD, 96f));
            add(label);
            setOpaque(false);
        }

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

    }

}

请参阅如何创建半透明和异形窗口

现在,如果你想填充窗口,然后删除行 frame.setBackground(new Color(0, 0, 0, 0));,这就是使框架透明的原因.

本文地址:https://www.itbaoku.cn/post/978676.html