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

查看:106
本文介绍了在屏幕上显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按下按钮时在屏幕上显示一条消息。该消息不应该有任何窗口,应该在屏幕的中心显示任何内容。有点像



JLayedPane



如果您想在当前帧中显示特定组件的内容,那么您可以采取 JLayeredPane 的优点,其作用类似于组件的玻璃窗格......



参见



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


I would like to display a message onto the screen upon the button press. The message should not have any window and should be displayed at the center of the screen over any content that would be there. Kind of like here. How would I go about achieving such an effect?

I have tried searching everywhere, but the nature of the question would not yield results. Thanks to anyone for their help in pointing me to right direction.

UPDATE: For those wondering. This is the project I am working on, thanks MadProgrammer for your help. You saved my eyes.

解决方案

I can think of at least three ways to do this in Swing...

GlassPane

If you simply want to show content over the current frame, then you can use the current frames glass pane...

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);
        }

    }

}

See how to use How to Use Root Panes

JLayedPane

If you want to display the content over a particular component within the current frame, then you could take advantage of the JLayeredPane, which acts a lot like a glass pane for components...

See How to Decorate Components with the JLayer Class for more details

Undecorated Frame

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);
        }

    }

}

See How to Create Translucent and Shaped Windows

Now, if you want the window be filled, then remove the line frame.setBackground(new Color(0, 0, 0, 0));, this is what makes the frame transparent.

这篇关于在屏幕上显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆