Java:Swing中的HTML,链接边距不起作用 [英] Java: HTML in Swing, link margin not working

查看:109
本文介绍了Java:Swing中的HTML,链接边距不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!
我正在尝试格式化Swing中的HTML内容。诸如颜色或文本修饰之类的格式正常工作。但是当谈到链接余量时它根本不起作用。

Hi there
I'm trying to format HTML content in Swing. Formattings such as color or text-decoration are working fine. But when it comes to margin of links it is not working at all.

这是我正在使用的所有CSS语法:

This is all the CSS syntax I'm using:

StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("a {font : arial; text-decoration: none; color: #0174DF; margin-left: 50px}");

除了 margin-left 之外的所有内容都是工作。我在浏览器中测试了语法,它在那里工作正常。我也尝试使用 text-align:left display:block 因为我发现一些文章指出链接边缘如果没有,就行不通。

everything, except the margin-left is working. I tested the syntax in a Browser and it works fine there. I also tried using text-align: left or display: block becouse I found some articles pointing out that margin of links would not work without.

使用以下HTML代码:

The following HTML code is used:

<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>

HTML代码由库生成,无法修改。

The HTML code is generated by a library and can't be modified.

根据我对HTML / CSS的理解,无法将 margin 样式信息添加到内联对象,如链接,因为margin-top或保证金底部是不可能的。 margin-left oder margin-right但不应该是一个问题。

From my understanding of HTML/CSS it is not possible to add margin style information to an inline object like a link becouse margin-top or margin-bottom is not possible. margin-left oder margin-right however should not be a problem.

thx

编辑:顺便说一下,我正在使用HTMLEditorKit。

edit: by the way, I'm using HTMLEditorKit.

推荐答案

这是我放弃或疯狂(或两者兼而有之)之前的最后一次尝试。

This is my last attempt before I give up or go mad (or both).

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 = "&nbsp;&nbsp;&nbsp;&nbsp";
        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);
    }
}



屏幕截图



Screenshot

这篇关于Java:Swing中的HTML,链接边距不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆