在Swing中,有时可能会出现一些较为复杂的样式表现需求,如果单纯通过代码实现,往往得不偿失。这时,我们就可以利用html标签,在组件中得到我们想要的效果。
例子如下:
package
org.loon.test;
import
java.awt.BorderLayout;
import
java.awt.Color;
import
java.awt.Desktop;
import
java.awt.Dimension;
import
java.awt.EventQueue;
import
java.awt.Font;
import
java.awt.Graphics;
import
java.awt.Graphics2D;
import
java.awt.GridLayout;
import
java.awt.color.ColorSpace;
import
java.awt.event.ActionEvent;
import
java.awt.image.BufferedImage;
import
java.awt.image.ColorConvertOp;
import
java.io.IOException;
import
java.net.URI;
import
java.net.URISyntaxException;
import
javax.swing.AbstractAction;
import
javax.swing.BorderFactory;
import
javax.swing.JCheckBox;
import
javax.swing.JComponent;
import
javax.swing.JEditorPane;
import
javax.swing.JFrame;
import
javax.swing.JLabel;
import
javax.swing.JPanel;
import
javax.swing.UIManager;
import
javax.swing.WindowConstants;
import
javax.swing.event.HyperlinkEvent;
import
javax.swing.event.HyperlinkListener;
/**
*<p>
*Title:LoonFramework
*</p>
*<p>
*Description:
*</p>
*<p>
*Copyright:Copyright(c)2007
*</p>
*<p>
*Company:LoonFramework
*</p>
*
*
@author
chenpeng
*@email:ceponline@yahoo.com.cn
*
@version
0.1
*/
public
class
HtmlLable
extends
JPanel
{
//
内置了本地系统桌面操作支持
private
Desktopdesktop;
private
static
final
long
serialVersionUID
=
1L
;
//
html内容
private
static
final
StringHTML_TEXT
=
"
<html><fontcolor='red'><ahref="http://www.helpsoff.com.cn"target="_blank">支持SOFF,插件无罪</a></font>
"
;
public
HtmlLable()
{
super
(
new
BorderLayout());
final
JLabellabel0
=
new
JLabel(
"
普通的JLabel
"
);
final
JLabellabel1
=
new
JLabel(HTML_TEXT);
final
JLabellabel2
=
new
JLabel(HTML_TEXT)
{
private
static
final
long
serialVersionUID
=
1L
;
//
设定Enabled样式
public
void
setEnabled(
boolean
b)
{
super
.setEnabled(b);
setForeground(b
?
(Color)UIManager.get(
"
Label.foreground
"
)
:(Color)UIManager.get(
"
Label.disabledForeground
"
));
}
}
;
final
JLabellabel3
=
new
JLabel(HTML_TEXT)
{
private
static
final
long
serialVersionUID
=
1L
;
//
阴影
private
BufferedImageshadow;
//
设定Enabled样式
public
void
setEnabled(
boolean
b)
{
setForeground(b
?
(Color)UIManager.get(
"
Label.foreground
"
)
:(Color)UIManager.get(
"
Label.disabledForeground
"
));
if
(
!
b)
{
BufferedImagesource
=
new
BufferedImage(getWidth(),
getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics2Dg2
=
source.createGraphics();
g2.setPaint(
new
Color(
0
,
0
,
0
,
0
));
g2.fillRect(
0
,
0
,getWidth(),getHeight());
paint(g2);
g2.dispose();
ColorConvertOpcolorConvert
=
new
ColorConvertOp(ColorSpace
.getInstance(ColorSpace.CS_GRAY),
null
);
shadow
=
colorConvert.filter(source,
null
);
}
super
.setEnabled(b);
}
public
void
paintComponent(Graphicsg)
{
if
(isEnabled())
{
super
.paintComponent(g);
}
else
{
if
(shadow
!=
null
)
g.drawImage(shadow,
0
,
0
,
this
);
}
}
}
;
/**
*设定为html
*/
final
JEditorPaneeditor1
=
new
MyHtmlEdit(
"
text/html
"
,HTML_TEXT);
editor1.setOpaque(
false
);
editor1.setEditable(
false
);
final
JEditorPaneeditor2
=
new
MyHtmlEdit(
"
text/html
"
,HTML_TEXT);
editor2.setOpaque(
false
);
editor2.setEditable(
false
);
editor2.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
Boolean.TRUE);
editor2.setFont((Font)UIManager.get(
"
Label.font
"
));
JPanelbox
=
new
JPanel(
new
GridLayout(
2
,
3
));
box.add(makePanel(
"
JLabel
"
,label0));
box.add(makePanel(
"
JLabel+Html
"
,label1));
box.add(makePanel(
"
JLabel+Html+
"
,label2));
box.add(makePanel(
"
JLabel+Html++
"
,label3));
box.add(makePanel(
"
JEditorPane
"
,editor1));
box.add(makePanel(
"
JEditorPane+
"
,editor2));
JCheckBoxcheck
=
new
JCheckBox(
"
setEnabled
"
,
true
);
check.setAction(
new
AbstractAction(
"
setEnabled
"
)
{
private
static
final
long
serialVersionUID
=
1L
;
public
void
actionPerformed(ActionEvente)
{
boolean
flag
=
((JCheckBox)e.getSource()).isSelected();
setVisible(
false
);
label0.setEnabled(flag);
label1.setEnabled(flag);
label2.setEnabled(flag);
label3.setEnabled(flag);
editor1.setEnabled(flag);
editor2.setEnabled(flag);
setVisible(
true
);
}
}
);
add(check,BorderLayout.NORTH);
add(box,BorderLayout.CENTER);
desktop
=
Desktop.getDesktop();
}
/**
*自定义一个JEditorPane,用以处理html事件
*
*
@author
chenpeng
*
*/
class
MyHtmlEdit
extends
JEditorPane
implements
HyperlinkListener
{
/**
*
*/
private
static
final
long
serialVersionUID
=
1L
;
public
MyHtmlEdit(Stringtype,Stringtext)
{
super
(type,text);
addHyperlinkListener(
this
);
}
public
void
hyperlinkUpdate(HyperlinkEvente)
{
if
(e.getEventType()
==
HyperlinkEvent.EventType.ACTIVATED)
{
try
{
//
在本地浏览器中打开uri(jdk1.6后由于强化了awt,就不用再自己写本地浏览器调用了)
desktop.browse(e.getURL().toURI());
}
catch
(IOExceptionex)
{
ex.printStackTrace();
}
catch
(URISyntaxExceptionex)
{
ex.printStackTrace();
}
}
}
}
/**
*创建面板
*
*
@param
title
*
@param
label
*
@return
*/
private
JPanelmakePanel(Stringtitle,JComponentlabel)
{
JPanelp
=
new
JPanel(
new
GridLayout(
1
,
1
));
p.setBorder(BorderFactory.createTitledBorder(title));
p.add(label);
return
p;
}
public
static
void
main(String[]args)
{
EventQueue.invokeLater(
new
Runnable()
{
public
void
run()
{
createGUI();
}
}
);
}
public
static
void
createGUI()
{
JFrameframe
=
new
JFrame(
"
在Swing中使用Html标签
"
);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(
new
Dimension(
450
,
200
));
frame.getContentPane().add(
new
HtmlLable());
frame.setResizable(
false
);
frame.setLocationRelativeTo(
null
);
//
jdk1.6新项,置顶
frame.setAlwaysOnTop(
true
);
frame.setVisible(
true
);
}
}
效果图:
这时的Swing,既可以实现Swing原有组件功能,也可以接受Html操作,可说是JavaGUI技术中为数不多的亮点之一。
例子如下:






































































































































































































































效果图:

这时的Swing,既可以实现Swing原有组件功能,也可以接受Html操作,可说是JavaGUI技术中为数不多的亮点之一。