我用我自己的方式来做这件事 -
package com.whodesire.element;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
public class MessageEdge {
private JDialog messageEdge = null;
private JDialog tempMessage = null;
private Point elmLoc = null;
private String[] option;
private JButton btnOK;
private JLayeredPane lp = null;
private int top = 70;
MessageEdge(JFrame owner){
owner.setAlwaysOnTop(false);
}
public void ShowMessage(final String msg){
ShowMessage(msg, "OK");
}
public void ShowMessage(final String msg, final String... option){
this.option = option;
init();
}
protected void setRootPaneStroking(JRootPane rootPane){
rootPane.setWindowDecorationStyle(JRootPane.NONE);
KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
Action action = new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
disposeMessage();
}
};
InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(stroke, "ESCAPE");
rootPane.getActionMap().put("ESCAPE", action);
};
private void init(){
top = 70;
tempMessage = new JDialog(){
private static final long serialVersionUID = 1L;
{
setUndecorated(true);
setType(Type.UTILITY);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setBackground(new Color(0, 0, 0, 0));
elmLoc = Elementos.getElementosLocationPoint();
setSize(460, 57);
setLayout(new GridLayout(1, 1));
if(lp == null)
lp = getModelJLayeredPane();
add(lp);
setLocation(elmLoc.x-10, elmLoc.y+top);
setAlwaysOnTop(true);
validate();
setVisible(true);
}
};
new Thread(new Runnable(){
public void run(){
try{
OneMethod.playSound("catchyMessage");
whileLoop:
while(true){
//System.out.println("top value is : " + top + ", is top value bigger than -47 : " + (top > -47));
if(top > -47){
top -= 7;
tempMessage.setLocation(elmLoc.x-10, elmLoc.y+top);
}else{
top = -47;
initMessageEdge();
break whileLoop;
}
Thread.sleep(4);
}
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}).start();
}
private void initMessageEdge(){
tempMessage.setVisible(false);
tempMessage.remove(lp);
tempMessage.dispose();
//Elementos is my JFrame
messageEdge = new JDialog(Elementos.getFrame(), true){
private static final long serialVersionUID = 1L;
{
setUndecorated(true);
setType(Type.UTILITY);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setRootPaneStroking(getRootPane());
setBackground(new Color(0, 0, 0, 0));
requestFocusInWindow();
elmLoc = Elementos.getElementosLocationPoint();
setSize(460, 57);
setLayout(new GridLayout(1, 1));
if(lp == null)
lp = getModelJLayeredPane();
add(lp);
setLocation(elmLoc.x-10, elmLoc.y+top);
setAlwaysOnTop(true);
validate();
}
};
messageEdge.setVisible(true);
messageEdge.requestFocus();
}
private JLayeredPane getModelJLayeredPane(){
JLayeredPane layer = new JLayeredPane();
layer.setBounds(0, 0, 460, 57);
layer.setLayout(null);
JPanel panel = new JPanel(){
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics gr){
super.paintComponent(gr);
Graphics2D g = (Graphics2D) gr;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, 0.01f));
g.setColor(new Color(255, 196, 51));
g.fillRect(0, 0, 460, 57);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f));
g.setColor(new Color(255, 196, 51));
g.fillRect(0, 0, 460, 47);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
g.setColor(new Color(244, 171, 50));
int[] xxPoly = {0, 10, 10};
int[] yyPoly = {47, 47, 57};
Polygon pplg = new Polygon(xxPoly, yyPoly, xxPoly.length);
g.fillPolygon(pplg);
}
};
panel.setSize(450, 57);
panel.setOpaque(true);
panel.setLayout(null);
layer.add(panel);
btnOK = new JButton(){
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics gr) {
final Graphics2D g = (Graphics2D) gr.create();
g.setPaint(new GradientPaint(new Point(0, 0), Color.WHITE,
new Point(0, getHeight()), new Color(255, 196, 51)));
g.fillRect(0, 0, getWidth(), getHeight());
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setColor(Color.BLACK);
g.setFont(new Font(OneMethod.getSegoeUIBoldFont().getFamily(), Font.BOLD, 11));
g.drawString(option[0], 8, 17);
g.dispose();
super.paintComponent(g);
}
};
btnOK.setFocusPainted(false);
btnOK.setBorder(BorderFactory.createLineBorder(new Color(244, 171, 50)));
btnOK.setBounds(460-30, (int)((47/2)-(25/2)), 30, 25);
layer.add(btnOK);
btnOK.addMouseListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent evt){
btnOK.setBorder(BorderFactory.createEmptyBorder());
btnOK.setBorder(BorderFactory.createLoweredBevelBorder());
}
@Override
public void mouseReleased(MouseEvent evt){
btnOK.setBorder(BorderFactory.createLineBorder(new Color(244, 171, 50)));
disposeMessage();
Elementos.getFrame().setAlwaysOnTop(true);
}
});
layer.moveToFront(btnOK);
return layer;
}
private void disposeMessage(){
messageEdge.setModal(false);
messageEdge.setVisible(false);
messageEdge.dispose();
}
}
希望它可以帮助你和其他人,干杯......