您好,
我正在尝试格式化Swing中的HTML内容.诸如颜色或文本修饰之类的格式正常工作.但是当涉及到链接余量时,它根本不起作用.
这是我正在使用的所有CSS语法:
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("a {font : arial; text-decoration: none; color: #0174DF; margin-left: 50px}");
Run Code Online (Sandbox Code Playgroud)
一切,除了margin-left
工作.我在浏览器中测试了语法,它在那里工作正常.我也试过使用text-align: left
或者display: block
因为我发现一些文章指出链接的边缘不会没有用.
使用以下HTML代码:
<html><head></head><body><div>
<a href="http://www.zhaw.ch" style="font-size: 50.24324324324324px">akamaitechnologies.com</a>
<a href="http://www.zhaw.ch" style="font-size: 17.37837837837838px">amazonaws.com</a>
<a href="http://www.zhaw.ch" style="font-size: 18.243243243243242px">cotendo.net</a>
<a href="http://www.zhaw.ch" style="font-size: 24.08108108108108px">facebook.com</a>
<a href="http://www.zhaw.ch" style="font-size: 17.594594594594597px">google.ch</a>
<a href="http://www.zhaw.ch" style="font-size: 55.0px">heise.de</a>
<a href="http://www.zhaw.ch" style="font-size: 16.08108108108108px">ip-plus.net</a>
<a href="http://www.zhaw.ch" style="font-size: 21.054054054054056px">ligatus.com</a>
</div></body></html>
Run Code Online (Sandbox Code Playgroud)
HTML代码由库生成,无法修改.
根据我对HTML/CSS的理解,无法将margin
样式信息添加到内联对象(如链接),因为margin-top或margin-bottom是不可能的.保证金 - 左边缘保证金 - 但不应该是一个问题.
谢谢
编辑:顺便说一下,我正在使用HTMLEditorKit.
这是我放弃或疯狂(或两者兼而有之)之前的最后一次尝试.
import javax.swing.*;
class TestHtmlIndent {
public static void main(String[] args) {
String raw =
"<html><head></head><body><div>" +
"<a href=\"http://a.b\" style=\"font-size: 20px\">akamaitechnologies.com</a>" +
"<a href=\"http://a.b\" style=\"font-size: 17px\">amazonaws.com</a>" +
"<a href=\"http://a.b\" style=\"font-size: 18px\">cotendo.net</a>" +
"<a href=\"http://a.b\" style=\"font-size: 24px\">facebook.com</a>" +
"<a href=\"http://a.b\" style=\"font-size: 17px\">google.ch</a>" +
"<a href=\"http://a.b\" style=\"font-size: 25px\">heise.de</a>" +
"<a href=\"http://a.b\" style=\"font-size: 16px\">ip-plus.net</a>" +
"<a href=\"http://a.b\" style=\"font-size: 21px\">ligatus.com</a>" +
"</div></body></html>";
String style =
"<style type='text/css'>" +
"body {width: 600px;}" +
".cloudLink {text-decoration: none; color: #0174DF; " +
"font-family: helvetica, arial, sans-serif;}" +
"</style>";
raw = raw.replace("<head></head>", "<head>" + style + "</head>");
String space4 = "  ";
String space20 = space4 + space4 + space4 + space4 + space4;
final String processed1 = raw.replace(
"<a ", space20 + "<a class='cloudLink' ");
Runnable r = new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, processed1);
}
};
SwingUtilities.invokeLater(r);
}
}
Run Code Online (Sandbox Code Playgroud)