---
title: "在Swing中使用Html标签"
author: "mmdev"
date: "2007-12-25T10:32:00Z"
tags: []
slug: "swing-html"
draft: false
lastmod: "2012-04-14T20:13:51Z"
id: 1486361
url: "https://mmdev.iteye.com/blog/1486361"
---

在Swing中，有时可能会出现一些较为复杂的样式表现需求，如果单纯通过代码实现，往往得不偿失。这时，我们就可以利用html标签，在组件中得到我们想要的效果。

例子如下：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.loon.test;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.BorderLayout;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Color;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Desktop;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Dimension;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.EventQueue;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Font;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Graphics;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Graphics2D;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.GridLayout;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.color.ColorSpace;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.event.ActionEvent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.image.BufferedImage;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.image.ColorConvertOp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.IOException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.net.URI;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.net.URISyntaxException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.AbstractAction;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.BorderFactory;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.JCheckBox;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.JComponent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.JEditorPane;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.JFrame;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.JLabel;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.JPanel;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.UIManager;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.WindowConstants;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.event.HyperlinkEvent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.swing.event.HyperlinkListener;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Title:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Description:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Copyright:Copyright(c)2007  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Company:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassHtmlLableextendsJPanel...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//内置了本地系统桌面操作支持  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateDesktopdesktop;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinallongserialVersionUID=1L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//html内容  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalStringHTML\_TEXT="&lt;html&gt;&lt;fontcolor='red'&gt;&lt;ahref="http://www.helpsoff.com.cn"target="\_blank"&gt;支持SOFF，插件无罪&lt;/a&gt;&lt;/font&gt;";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicHtmlLable()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)super(newBorderLayout());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)finalJLabellabel0=newJLabel("普通的JLabel");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)finalJLabellabel1=newJLabel(HTML\_TEXT);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)finalJLabellabel2=newJLabel(HTML\_TEXT)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinallongserialVersionUID=1L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定Enabled样式  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidsetEnabled(booleanb)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)super.setEnabled(b);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)setForeground(b?(Color)UIManager.get("Label.foreground")  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif):(Color)UIManager.get("Label.disabledForeground"));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)finalJLabellabel3=newJLabel(HTML\_TEXT)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinallongserialVersionUID=1L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//阴影  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateBufferedImageshadow;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定Enabled样式  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidsetEnabled(booleanb)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)setForeground(b?(Color)UIManager.get("Label.foreground")  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif):(Color)UIManager.get("Label.disabledForeground"));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(!b)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)BufferedImagesource=newBufferedImage(getWidth(),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)getHeight(),BufferedImage.TYPE\_INT\_ARGB);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Graphics2Dg2=source.createGraphics();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.setPaint(newColor(0,0,0,0));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.fillRect(0,0,getWidth(),getHeight());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)paint(g2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g2.dispose();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ColorConvertOpcolorConvert=newColorConvertOp(ColorSpace  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif).getInstance(ColorSpace.CS\_GRAY),null);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)shadow=colorConvert.filter(source,null);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)super.setEnabled(b);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidpaintComponent(Graphicsg)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(isEnabled())...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)super.paintComponent(g);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}else...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(shadow!=null)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.drawImage(shadow,0,0,this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*设定为html  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)finalJEditorPaneeditor1=newMyHtmlEdit("text/html",HTML\_TEXT);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)editor1.setOpaque(false);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)editor1.setEditable(false);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)finalJEditorPaneeditor2=newMyHtmlEdit("text/html",HTML\_TEXT);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)editor2.setOpaque(false);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)editor2.setEditable(false);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)editor2.putClientProperty(JEditorPane.HONOR\_DISPLAY\_PROPERTIES,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Boolean.TRUE);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)editor2.setFont((Font)UIManager.get("Label.font"));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)JPanelbox=newJPanel(newGridLayout(2,3));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)box.add(makePanel("JLabel",label0));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)box.add(makePanel("JLabel+Html",label1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)box.add(makePanel("JLabel+Html+",label2));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)box.add(makePanel("JLabel+Html++",label3));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)box.add(makePanel("JEditorPane",editor1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)box.add(makePanel("JEditorPane+",editor2));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)JCheckBoxcheck=newJCheckBox("setEnabled",true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)check.setAction(newAbstractAction("setEnabled")...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinallongserialVersionUID=1L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidactionPerformed(ActionEvente)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)booleanflag=((JCheckBox)e.getSource()).isSelected();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)setVisible(false);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)label0.setEnabled(flag);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)label1.setEnabled(flag);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)label2.setEnabled(flag);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)label3.setEnabled(flag);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)editor1.setEnabled(flag);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)editor2.setEnabled(flag);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)setVisible(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)});  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)add(check,BorderLayout.NORTH);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)add(box,BorderLayout.CENTER);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)desktop=Desktop.getDesktop();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*自定义一个JEditorPane，用以处理html事件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)classMyHtmlEditextendsJEditorPaneimplementsHyperlinkListener...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinallongserialVersionUID=1L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicMyHtmlEdit(Stringtype,Stringtext)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)super(type,text);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)addHyperlinkListener(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidhyperlinkUpdate(HyperlinkEvente)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//在本地浏览器中打开uri(jdk1.6后由于强化了awt,就不用再自己写本地浏览器调用了)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)desktop.browse(e.getURL().toURI());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(IOExceptionex)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ex.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(URISyntaxExceptionex)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ex.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*创建面板  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramtitle  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramlabel  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@return  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)privateJPanelmakePanel(Stringtitle,JComponentlabel)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)JPanelp=newJPanel(newGridLayout(1,1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)p.setBorder(BorderFactory.createTitledBorder(title));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)p.add(label);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidmain(String\[]args)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)EventQueue.invokeLater(newRunnable()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidrun()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)createGUI();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)});  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidcreateGUI()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)JFrameframe=newJFrame("在Swing中使用Html标签");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setDefaultCloseOperation(WindowConstants.EXIT\_ON\_CLOSE);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setSize(newDimension(450,200));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.getContentPane().add(newHtmlLable());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setResizable(false);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setLocationRelativeTo(null);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//jdk1.6新项,置顶  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setAlwaysOnTop(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frame.setVisible(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

效果图：  
![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/swingtable.GIF)

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