Java Swing - Notification
package com.backup.swing; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Toolkit; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.WindowConstants; public class Notification { public static void show(String title, String message) { JFrame frame = new JFrame(); frame.setUndecorated(true); frame.setSize(300,125); frame.setAlwaysOnTop(true); frame.setLayout(new GridBagLayout()); Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();// size of the screen Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(frame.getGraphicsConfiguration());// height of the task bar frame.setLocation(scrSize.width - frame.getWidth(), scrSize.height - toolHeight.bottom - frame.getHeight()); GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = 0; constraints.gridy = 0; constraints.weightx = 1.0f; constraints.weighty = 1.0f; constraints.insets = new Insets(5, 5, 5, 5); constraints.fill = GridBagConstraints.BOTH; JLabel headingLabel = new JLabel(title); headingLabel.setIcon(new ImageIcon("/res/About32.png")); headingLabel.setOpaque(false); frame.add(headingLabel, constraints); constraints.gridx++; constraints.weightx = 0f; constraints.weighty = 0f; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTH; JButton closeButton = new JButton( new AbstractAction() { @Override public void actionPerformed(final ActionEvent e) { frame.dispose(); }}); closeButton.setText("X"); closeButton.setMargin(new Insets(1, 4, 1, 4)); closeButton.setFocusable(false); frame.add(closeButton, constraints); constraints.gridx = 0; constraints.gridy++; constraints.weightx = 1.0f; constraints.weighty = 1.0f; constraints.insets = new Insets(5, 5, 5, 5); constraints.fill = GridBagConstraints.BOTH; JLabel messageLabel = new JLabel("<html>"+message); //messageLabel.setBackground(Color.YELLOW); frame.add(messageLabel, constraints); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //frame.setBackground(Color.YELLOW); frame.setVisible(true); new Thread(){ @Override public void run() { try { Thread.sleep(5000); // time after which pop up will be disappeared. frame.dispose(); } catch (InterruptedException e) { e.printStackTrace(); } }; }.start(); } public static void main(String[] args) { String title = "This is header of notification message"; String message = "You got a new notification message. Isn't it awesome to have such a notification message."; Notification.show(title, message); } }
댓글 없음:
댓글 쓰기