代码片段
BufferedImage image=createTransparentImage("99");
if (image != null) {
g.drawImage(image, getWidth()-image.getWidth()+2, 0, 0,
null);
}
/**
* 创建图片
*
* @param os
* @param text
* @throws IOException
*/
public static BufferedImage createTransparentImage(String text) {
// 创建空白图片
BufferedImage image = new BufferedImage(15, 15, BufferedImage.TYPE_INT_ARGB);
// 获取图片画笔
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 设置水印文字颜色
g.setColor(Color.red);
//设置水印文字Font
g.setFont( new Font("PingFang SC Regular", Font.PLAIN, 10));
g.fillOval(0, 0, 15, 15);
// 设置水印文字颜色
g.setColor(Color.WHITE);
g.drawString(text,1, 12);
return image;
}
类使用
package nc.ui.pub.beans;
/**
* 功能:UILable,封装了部分属性的预设值 注意:在可视化状态应当首先设置ILabelType,然后再设置其他属性.
* 确保生成代码时先执行setILabelType 作者:张扬
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.font.FontRenderContext;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.util.UUID;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import nc.ui.pub.style.Style;
import nc.uitheme.ui.ThemeResourceCenter;
public class UILabel extends JLabel {
/**
*
*/
private static final long serialVersionUID = 3725886372687247057L;
public final static Color notNullColor = new Color(52, 102, 153);
/** 类型 */
public static final int JAVAX_DEFAULT_TYPE = 0;
/** JAVAX默认值类型 */
private int fieldILabelType = 1;
public static final int STYLE_DEFAULT = 1;
public static final int STYLE_TITLE_REPORT = 2;
public static final int STYLE_TITLE_BILL = 3;
public static final int STYLE_TITLE_SMALL = 4;
public static final int STYLE_BLACKBLUE = 5;
public static final int STYLE_NOTNULL = 6;
private boolean isHyperlinkLabel = false;
//add by lzul 20230423 附件是否已上传
private boolean isUpload= false;
private String url;
private Rectangle rect;
private Cursor normalCursor;
private Cursor handCursor;
private MouseMotionListener mouseMotionListener = null;
private MouseAdapter mouseAdaper = null;
static class LabelStyle {
Font font;
Color forground;
int hAlignment;
int vAlignment;
}
private static LabelStyle createLabelStyle(int type) {
String fname = Style.getFontname();
Font f = new Font(fname, Font.PLAIN, 12);
Color c = Style.getCtrFgColor();
int ha = LEFT;
int va = CENTER;
LabelStyle ls = new LabelStyle();
switch (type) {
case JAVAX_DEFAULT_TYPE:
break;
case STYLE_DEFAULT:
f = new Font(fname, Font.PLAIN, 12);
ha = LEFT;
va = CENTER;
break;
case STYLE_TITLE_REPORT:
f = new Font(fname, Font.PLAIN, 18);
c = notNullColor;
ha = CENTER;
va = CENTER;
break;
case STYLE_TITLE_BILL:
f = new Font(fname, Font.PLAIN, 18);
c = notNullColor;
ha = CENTER;
va = CENTER;
break;
case STYLE_TITLE_SMALL:
f = new Font(fname, Font.PLAIN, 14);
c = java.awt.Color.black;
ha = CENTER;
va = CENTER;
break;
case STYLE_BLACKBLUE:
f = new Font(fname, Font.PLAIN, 12);
c = notNullColor;
ha = LEFT;
va = CENTER;
break;
case STYLE_NOTNULL:
f = new Font(fname, Font.PLAIN, 12);
c = notNullColor;
ha = LEFT;
va = CENTER;
break;
}
ls.font = f;
ls.forground = c;
ls.hAlignment = ha;
ls.vAlignment = va;
// ls.height = h;
return ls;
}
// private boolean fieldTranslate = true;
public UILabel() {
super();
initialize();
}
public UILabel(java.lang.String p0) {
super(p0);
initialize();
}
public UILabel(java.lang.String p0, String url) {
super(p0);
this.url = url;
setHyperlinkLabel(true);
initialize();
}
public UILabel(java.lang.String p0, int p1) {
super(p0, p1);
initialize();
}
public UILabel(java.lang.String p0, javax.swing.Icon p1, int p2) {
super(p0, p1, p2);
initialize();
}
public UILabel(javax.swing.Icon p0) {
super(p0);
initialize();
}
public UILabel(javax.swing.Icon p0, int p1) {
super(p0, p1);
initialize();
}
/**
* 获取 iLabelType 特性 (int) 值.
*
* @return iLabelType 的特性值.
* @see #setILabelType
*/
public int getILabelType() {
return fieldILabelType;
}
/**
*
* 创建日期:(2001-3-2 12:41:37)
*
* @return java.lang.String
*/
public java.lang.String getToolTipText() {
return TooltipUtil.getTip4Label(this, super.getToolTipText());
}
/**
*
* 创建日期:(2001-4-27 19:13:52)
*
* @return java.lang.String
*/
public String getToolTipText(MouseEvent e) {
return getToolTipText();
}
/**
*
* 创建日期:(2002-3-21 10:30:55)
*
* @return java.lang.String
*/
public String getUntranslatedText() {
return super.getText();
}
/**
* 系统预设部分属性,并且可修改 创建日期:(2001-2-21 16:39:33)
*/
public void initialize() {
rect = new Rectangle();
normalCursor = new Cursor(Cursor.DEFAULT_CURSOR);
handCursor = new Cursor(Cursor.HAND_CURSOR);
setILabelType0(fieldILabelType);
javax.swing.ToolTipManager toolTipManager = javax.swing.ToolTipManager
.sharedInstance();
toolTipManager.registerComponent(this);
mouseMotionListener = new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
doIt(e);
}
public void mouseMoved(MouseEvent e) {
doIt(e);
}
private void doIt(MouseEvent e) {
//jingli修改,原来的代码在设置了鼠标图标后不起作用
Cursor cursor = getCursor();
if(isHyperlinkLabel()){
if (rect.contains(e.getPoint())) {
setCursor(handCursor);
} else {
setCursor(cursor);
}
} else {
setCursor(cursor);
}
}
};
mouseAdaper = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (isHyperlinkLabel() && SwingUtilities.isLeftMouseButton(e)) {
if (e.getClickCount() == 1) {
if (rect.contains(e.getPoint())) {
// fire event;
HyperlinkLabelEvent event = new HyperlinkLabelEvent(
this, getURL());
fireHyperLinklabelEvent(event);
}
}
}
}
};
addMouseMotionListener(mouseMotionListener);
addMouseListener(mouseAdaper);
}
/**
* 获取 translate 特性 (boolean) 值.
*
* @return translate 特性值.
* @see #setTranslate
*/
public boolean isTranslate() {
return true;
}
/**
* 设置 iLabelType 特性 (int) 的值.
*
* @param iLabelType
* 特性的新值.
* @see #getILabelType
*/
public void setILabelType(int iLabelType) {
if (fieldILabelType != iLabelType)
setILabelType0(iLabelType);
}
private void setILabelType0(int iLabelType) {
fieldILabelType = iLabelType;
LabelStyle ls = createLabelStyle(iLabelType);
setFont(ls.font);
setForeground(ls.forground);
setHorizontalAlignment(ls.hAlignment);
setVerticalAlignment(ls.vAlignment);
}
/**
* 设置 translate 特性 (boolean) 值.
*
* @param translate
* 新的特性值.
* @see #getTranslate
*/
public void setTranslate(boolean translate) {
}
public boolean isUpload() {
return isUpload;
}
public void setUpload(boolean isUpload) {
this.isUpload = isUpload;
}
public boolean isHyperlinkLabel() {
return isHyperlinkLabel;
}
public void setHyperlinkLabel(boolean isHyperlinkLabel) {
// if(mouseMotionListener != null){
// this.removeMouseMotionListener(mouseMotionListener);
// this.removeMouseListener(mouseAdaper);
// }
this.isHyperlinkLabel = isHyperlinkLabel;
// if (isHyperlinkLabel) {
// mouseMotionListener = new MouseMotionListener() {
// public void mouseDragged(MouseEvent e) {
// doIt(e);
// }
//
// public void mouseMoved(MouseEvent e) {
// doIt(e);
// }
//
// private void doIt(MouseEvent e) {
// if (rect.contains(e.getPoint())) {
// setCursor(handCursor);
// } else {
// setCursor(normalCursor);
// }
// }
// };
// mouseAdaper = new MouseAdapter() {
// public void mouseClicked(MouseEvent e) {
// if (SwingUtilities.isLeftMouseButton(e)) {
// if (e.getClickCount() == 1) {
// if (rect.contains(e.getPoint())) {
// // fire event;
// HyperlinkLabelEvent event = new HyperlinkLabelEvent(
// this, getURL());
// fireHyperLinklabelEvent(event);
// }
// }
// }
// }
// };
// this.addMouseMotionListener(mouseMotionListener);
// this.addMouseListener(mouseAdaper);
// } else {
// this.removeMouseMotionListener(mouseMotionListener);
// this.removeMouseListener(mouseAdaper);
// setCursor(normalCursor);
// }
}
/**
* 创建图片
*
* @param os
* @param text
* @throws IOException
*/
public static BufferedImage createTransparentImage(String text) {
// 创建空白图片
BufferedImage image = new BufferedImage(15, 15, BufferedImage.TYPE_INT_ARGB);
// 获取图片画笔
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 设置水印文字颜色
g.setColor(Color.red);
//设置水印文字Font
g.setFont( new Font("PingFang SC Regular", Font.PLAIN, 10));
g.fillOval(0, 0, 15, 15);
// 设置水印文字颜色
g.setColor(Color.WHITE);
g.drawString(text,1, 12);
return image;
}
public void paint(Graphics g) {
if (!isHyperlinkLabel()) {
super.paint(g);
return;
}
// super.paintBorder(g);
String text = this.getText();
setText("");
super.paint(g);
setText(text);
if (this.getText() == null || this.getText().length() == 0)
return;
Graphics2D g2 = (Graphics2D) g;
Font f = this.getFont();
AttributedString ats = new AttributedString(this.getText());
ats.addAttribute(TextAttribute.FONT, f);
ats.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
AttributedCharacterIterator iter = ats.getIterator();
FontRenderContext frc = g2.getFontRenderContext();
TextLayout tl = new TextLayout(iter, frc);
if(isUpload()) {
g2.setColor(Style.getHyperlinkForeGround());
BufferedImage image=createTransparentImage("99");
if (image != null) {
g.drawImage(image, getWidth()-image.getWidth()+2, 0,// 0,
null);
}
}
g2.setColor(Style.getHyperlinkForeGround());
Rectangle rect = tl.getBounds().getBounds();
Rectangle total = this.getBounds();
Insets insets = getInsets();
this.rect.y = (total.height - rect.height - insets.top - insets.bottom) / 2;
if (getHorizontalAlignment() == LEFT) {
this.rect.x = insets.left;
} else if (getHorizontalAlignment() == CENTER) {
this.rect.x = (total.width - rect.width - insets.left - insets.right) / 2;
} else if (getHorizontalAlignment() == RIGHT) {
this.rect.x = total.width - rect.width - insets.left - insets.right;
}
this.rect.width = rect.width;
this.rect.height = rect.height;
tl.draw(g2, this.rect.x, this.rect.y + rect.height);
}
public void addHyperlinkLabelListener(HyperlinkLabelListener aListener) {
listenerList.add(HyperlinkLabelListener.class, aListener);
}
public void removeHyperlinkLabelListener(HyperlinkLabelListener aListener) {
listenerList.remove(HyperlinkLabelListener.class, aListener);
}
private void fireHyperLinklabelEvent(HyperlinkLabelEvent event) {
HyperlinkLabelListener[] listeners = listenerList
.getListeners(HyperlinkLabelListener.class);
if (listeners.length == 0) {
HyperLinkHandler.openHyperLink(event.getUrl());
} else {
for (HyperlinkLabelListener l : listeners) {
l.hyperlinkClicked(event);
}
}
}
public String getURL() {
return url;
}
public void setURL(String url) {
this.url = url;
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame frame = new JFrame("HyperlinkLabel");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
final UILabel label = new UILabel("263.net", "http://www.263.net/");
label.addHyperlinkLabelListener(new HyperlinkLabelListener() {
public void hyperlinkClicked(HyperlinkLabelEvent event) {
// TODO Auto-generated method stub
label.setText("label is clicked and the hyperlink is "
+ event.getUrl().toString());
// label.setHyperlinkLabel(false);
}
});
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setLocation(100, 100);
frame.setVisible(true);
}
}