2015-01-17 18:19:38|?次阅读|上传:wustguangh【已有?条评论】发表评论
关键词:Java, Swing, 界面设计|来源:唯设编程网
首先调用了initStyle函数对显示样式进行初始化,第二步定义了超链接的监听器,该例子用到的超链接按钮就一个“保存快照”按钮,用户点击该按钮可以将当前状态保存,以备下次查看。用户点击超链接触发的事件类型是EventType.ACTIVATED,除此之外,用户将鼠标移动到超链接上方会触发EventType.ENTERED时间,离开则会触发EventType.EXITED事件。我们利用这两个事件实现超链接样式的动态改变。这一步工作我们使用了Document的成员函数setCharacterAttributes来实现。
在最后调用了JEditorPane的setEditable(false)设置为HTML显示面板不可编辑,setEditorKti用来应用HTMLEditorKit到JEditorPane,addHyperlinkListener用来为超链接添加监听器,setDocument用来绑定JEditorPane显示的HTML文档。
/**
* 初始化css样式
* @param styleSheet
*/
private void initStyle(StyleSheet styleSheet) {
styleSheet.addRule("h1 {"
+ "margin:0px;"
+ "padding:0px;"
+ "text-align:center;"
+ "height:15px;"
+ "font-size:12px"
+ "}");
styleSheet.addRule("h2 {"
+ "margin:0 0 5px 0;"
+ "padding:0px;"
+ "font-size:11px;"
+ "font-weight:normal"
+ "}");
styleSheet.addRule("table {"
+ "background-color:#000;"
+ "font-size:10px;"
+ "}");
styleSheet.addRule("table td{"
+ "width:250px;"
+ "background-color:#ffffff;"
+ "}");
styleSheet.addRule("table th {"
+ "width:250px;"
+ "background-color:#ffffff;"
+ "font-weight:normal"
+ "}");
styleSheet.addRule(".col1{"
+ "width:50px;"
+ "text-align:center;"
+ "}");
styleSheet.addRule(".col2{"
+ "text-align:center;"
+ "}");
styleSheet.addRule(".col3{"
+ "text-align:center;"
+ "}");
styleSheet.addRule(".col4{"
+ "text-align:center;"
+ "}");
styleSheet.addRule(".btn{"
+ "background-color:#04819c;"
+ "color:#FFFFFF;"
+ "text-decoration:none;"
+ "font-size:12px;"
+ "}");
styleSheet.addRule(".btn:hover{"
+ "background-color:#05a3c5;"
+ "}");
styleSheet.addRule("div.tlb{"
+ "background-color:#eeeeee;"
+ "text-align:center;"
+ "padding:5px;"
+ "}");
styleSheet.addRule("p{"
+ "margin:5px;"
+ "}");
}
这部分内容没有什么可以介绍的,主要都是些CSS样式定义,根据你的具体需求编写即可。