【JAVA Swing】自定义弹出的无边提醒框(可自动消失)

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.Toolkit;



import javax.swing.JLabel;

import javax.swing.WindowConstants;



public class showMessageFrame extends javax.swing.JFrame {

    private JLabel text;

    Toolkit tk = Toolkit.getDefaultToolkit();

    Dimension screensize = tk.getScreenSize();

    int height = screensize.height;

    int width = screensize.width;

    private String str = null;



    public showMessageFrame(String str) {

        this.str = str;

        new Thread(new Runnable() {

            @Override

            public void run() {

                initGUI();

            }

        }).start();

    }



    private void initGUI() {

        try {

            setUndecorated(true);

            setLocationRelativeTo(null);

            setVisible(true);

            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

            {

                text = new JLabel("<html>" + str + "</html>", JLabel.CENTER);

                getContentPane().add(text, BorderLayout.CENTER);

                text.setBackground(new java.awt.Color(255, 251, 240));

            }

            pack();

            setBounds(width / 2 - 180, height - 150, 360, 100);

            try {

                Thread.sleep(2000);

            } catch (InterruptedException e1) {

                e1.printStackTrace();

            }

            dispose();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }



}

Swing弹出的提醒无边框,换行使用<br>,2s后自动消失。

【JAVA Swing】自定义弹出的无边提醒框(可自动消失)

你可能感兴趣的:(java swing)