系统消息框

系统消息弹出框实现方式 一种:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/**
 * 
 * @author luojialin
 * @2014-2014-3-8 下午2:14:18
 */
public class MessageManager extends JWindow implements Runnable {
 private static final long serialVersionUID = 1291149350784042880L;
 private static MessageManager message;
 private JPanel panel1;
 private JPanel titlePane;
 private JTextPane messagePane;
 private JLabel lblTitle;
 private JLabel time;

 private Integer screenWidth; // 屏幕宽度
 private Integer screenHeight; // 屏幕高度
 private Integer windowWidth = 200; // 设置提示窗口宽度
 private Integer windowHeight = 150; // 设置提示窗口高度
 private Integer bottmToolKitHeight; // 底部任务栏高度,如果没有任务栏则为零
 private Integer stayTime = 4000; // 提示框停留时间

 private Integer x; // 窗口起始X坐标
 private Integer y; // 窗口起始Y坐标

 private MessageManager() {
  initGUI();
 }

 public static MessageManager getInstance() {

  message = new MessageManager();

  return message;
 }

 private void initGUI() {

  bottmToolKitHeight = Toolkit.getDefaultToolkit().getScreenInsets(
    this.getGraphicsConfiguration()).bottom;
  Dimension dimension = PCSSWorkbench.getInstance().getSize();
  // Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
  screenWidth = dimension.width;
  screenHeight = dimension.height;

  x = screenWidth - windowWidth - 16;
  y = screenHeight - 75;
  this.setLocation(x, y);
  this.setLayout(new BorderLayout());
  panel1 = new JPanel();
  getContentPane().add(panel1, BorderLayout.CENTER);
  panel1.setLayout(new BorderLayout());

  lblTitle = new JLabel();
  titlePane = new JPanel();
  lblTitle.setForeground(Color.WHITE);
  lblTitle.setFont(new Font("微软雅黑",Font.BOLD,15));
  JLabel closeLabe = new JLabel("X"); 
  closeLabe.setFont(new Font("微软雅黑",Font.BOLD,15));
  closeLabe.addMouseListener(new MouseAdapter(){
   public void mouseClicked(MouseEvent e){
    dispose();
   }
  });
  titlePane = new JPanel(new BorderLayout());
  titlePane.add(lblTitle,BorderLayout.WEST);
  titlePane.add(closeLabe,BorderLayout.EAST);

  messagePane = new JTextPane();

  time = new JLabel();
  panel1.add(titlePane, BorderLayout.NORTH);
  panel1.add(messagePane, BorderLayout.CENTER);
  panel1.add(time, BorderLayout.SOUTH);

  this.setSize(windowWidth, windowHeight);
  this.setLocation(x, y);
  this.setAlwaysOnTop(true);
  this.setVisible(true);

 }

 private ImageIcon setIcon(String name) {
  ImageIcon icon = new ImageIcon(getClass().getClassLoader().getResource(
    "com/sf/module/pcss/core/client/message/img/" + name));
  return icon;
 }

 
 private void displayMessage(String title,String message,Color color,String icon){
  
  Thread thread = new Thread(this);
  thread.start();
  
  lblTitle.setText("  " + title);
  titlePane.setBackground(color);
  
  messagePane.setText(message);
  messagePane.setForeground(color);
  StyledDocument doc = (StyledDocument) messagePane.getDocument();
  Style style = doc.addStyle("StyleName", null);
  StyleConstants.setIcon(style, setIcon(icon));
  
  try {
   doc.insertString(doc.getLength(), message, style);
  } catch (BadLocationException e) {
   e.printStackTrace();
  }
  
  time.setText(DateUtils.getGreenWich());
  time.setHorizontalAlignment(SwingConstants.RIGHT);
  time.setForeground(color);
  panel1.setBorder(BorderFactory.createLineBorder(color));
 }
 
 /**
  * 操作成功信息提醒
  * 
  * @param title 
  * @param content
  */
 public void info(String title, String message) {
  
  Color color = new Color(0, 128, 0);
  String icon = "success.png";
  
  if (title == null || "".equals(title))
   title = Messages.getString("skin.message.title","提示");
  if (message == null || "".equals(message.trim())) 
   message = Messages.getString(message,"提示");
   
  displayMessage(title,message,color,icon);
  
 }

 /**
  * 错误提醒
  * 
  * @param title
  * @param content
  */
 public void error(String title, String message) {
  
  Color color = new Color(255, 0, 0);
  String icon = "fail.png";
  
  if (title == null || "".equals(title))
   title = Messages.getString("error","错误");
  if (message == null || "".equals(message.trim())) 
   message = Messages.getString(message,"错误");
   
  displayMessage(title,message,color,icon);
  
  
 }

 /**
  * 警告提醒
  * 
  * @param title
  * @param content
  */
 public void warn(String title, String message) {
  
  Color color = new Color(255,153,0);
  String icon = "warn.png";
  
  if (title == null || "".equals(title))
   title = Messages.getString("Warm","警告");
  if (message == null || "".equals(message.trim())) 
   message = Messages.getString("Warm","警告");
   
  displayMessage(title,message,color,icon);
  
 }

 @Override
 public void run() {
  Integer delay = 10;
  Integer step = 1;
  Integer end = windowHeight - bottmToolKitHeight - 20;
  while (true) {
   try {
    step++;
    y = y - 1;
    this.setLocation(x, y);
    if (step > end) {
     Thread.sleep(stayTime);
     break;
    }
    Thread.sleep(delay);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  step = 1;
  while (true) {
   try {
    step++;
    y = y + 1;
    this.setLocation(x, y);
    if (step > end) {
     this.dispose();
     break;
    }
    Thread.sleep(delay);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  this.dispose();
  // System.exit(0);
 }


}
  二种:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;



/**
 * 
 * @author luojialin
 * @2014-2014-3-8 下午2:14:18
 */
public class MessageManager extends JWindow  {
	private static final long serialVersionUID = 1291149350784042880L;
	private  MessageManager message;
	private JPanel panel1;
	private JPanel titlePane;
	private JTextPane messagePane;
	private JLabel lblTitle;
	private JLabel time;

	private Integer windowWidth = 200; // 设置提示窗口宽度
	private Integer windowHeight = 150; // 设置提示窗口高度
	private Integer stayTime = 3000; // 提示框停留时间

	private Integer x; // 窗口起始X坐标
	private Integer y; // 窗口起始Y坐标

	Timer timer ;
	
	public MessageManager() {
		initGUI();
	}

	public static MessageManager getInstance() {

		MessageManager messagew = new MessageManager();
		messagew.setMessage(messagew);

		return messagew;
	}

	private void initGUI() {

		this.setLayout(new BorderLayout());
		panel1 = new JPanel();
		getContentPane().add(panel1, BorderLayout.CENTER);
		panel1.setLayout(new BorderLayout());

		lblTitle = new JLabel();
		titlePane = new JPanel();
		lblTitle.setForeground(Color.WHITE);
		lblTitle.setFont(new Font("微软雅黑",Font.BOLD,15));
		JLabel closeLabe = new JLabel("X"); 
		closeLabe.setFont(new Font("微软雅黑",Font.BOLD,15));
		closeLabe.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e){
				dispose();
			}
		});
		titlePane = new JPanel(new BorderLayout());
		titlePane.add(lblTitle,BorderLayout.WEST);
		titlePane.add(closeLabe,BorderLayout.EAST);

		messagePane = new JTextPane();

		time = new JLabel();
		panel1.add(titlePane, BorderLayout.NORTH);
		panel1.add(messagePane, BorderLayout.CENTER);
		panel1.add(time, BorderLayout.SOUTH);


		this.setSize(windowWidth, windowHeight);
		
		Point p = PCSSWorkbench.getInstance().getLocationOnScreen();
		Dimension d = PCSSWorkbench.getInstance().getSize();
		x = (int) (p.x + d.getWidth() - windowWidth - 10);
		y = (int) (p.y + d.getHeight()- windowHeight - 10);
		this.setLocation(x, y);
		this.setAlwaysOnTop(true);
		this.setVisible(true);

	}

	private ImageIcon setIcon(String name) {
		ImageIcon icon = new ImageIcon(getClass().getClassLoader().getResource(
				"com/sf/module/pcss/core/message/img/" + name));
		return icon;
	}

	
	
	/**
	 * 操作成功信息提醒
	 * 
	 * @param title 
	 * @param content
	 */
	public void info(String title, String message) {
		
		Color color = new Color(0, 128, 0);
		String icon = "success.png";
		
		if (title == null || "".equals(title))
			title = Messages.getString("info","提示");
		if (message == null || "".equals(message.trim())) 
			message = Messages.getString(message,"提示");
			
		displayMessage(title,message,color,icon);
		
	}

	/**
	 * 错误提醒
	 * 
	 * @param title
	 * @param content
	 */
	public void error(String title, String message) {
		
		Color color = new Color(255, 0, 0);
		String icon = "fail.png";
		
		if (title == null || "".equals(title))
			title = Messages.getString("error","错误");
		if (message == null || "".equals(message.trim())) 
			message = Messages.getString(message,"错误");
			
		displayMessage(title,message,color,icon);
		
		
	}

	/**
	 * 警告提醒
	 * 
	 * @param title
	 * @param content
	 */
	public void warn(String title, String message) {
		
		Color color = new Color(255,153,0);
		String icon = "warn.png";
		
		if (title == null || "".equals(title))
			title = Messages.getString("Warm","警告");
		if (message == null || "".equals(message.trim())) 
			message = Messages.getString("Warm","警告");
			
		displayMessage(title,message,color,icon);
		
		
	}

	private void displayMessage(String title,String message,Color color,String icon){
		
		lblTitle.setText("  " + title);
		titlePane.setBackground(color);
		
		messagePane.setText(message);
		messagePane.setForeground(color);
		StyledDocument doc = (StyledDocument) messagePane.getDocument();
		Style style = doc.addStyle("StyleName", null);
		StyleConstants.setIcon(style, setIcon(icon));
		
		try {
			doc.insertString(doc.getLength(), message, style);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		
		time.setText(DateUtils.getGreenWich());
		time.setHorizontalAlignment(SwingConstants.RIGHT);
		time.setForeground(color);
		panel1.setBorder(BorderFactory.createLineBorder(color));
		
		
		timer = new Timer();
		timer.schedule(new MyTask(), stayTime);
	}
	
	class MyTask extends TimerTask{

		@Override
		public void run() {
			//message.setLocation(x, y);
			message.setVisible(false);
			message.dispose();
			timer.cancel();
		}
	}
	

	public MessageManager getMessage() {
		return message;
	}

	public void setMessage(MessageManager message) {
		this.message = message;
	}
	

}
   

猜你喜欢

转载自gxblluojialin.iteye.com/blog/2028287