【问题标题】:Swing HTML rendering shows very large bullet pointSwing HTML 渲染显示非常大的项目符号点
【发布时间】:2012-11-17 13:41:56
【问题描述】:

我使用 JEditorPane 在 swing 应用程序中呈现一些 HTML。我使用了一个项目符号列表<ul><li>...</li></ul>,我在输出中获得了过大的项目符号。相同的 HTML 块将在真实浏览器中显示正常大小的项目符号。

我在 Windows 7 / JDK 7 和 iirc 上也观察到了这一点,也在 Ubuntu 和 OpenJDK 1.7.0_09 上观察到了这一点。

这是已知的吗?有办法解决吗?

工作示例:

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

/**
 *
 */
public class HTMLTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // create new frame
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setSize(500, 500);

        // create editor pane and fill with some html
        JEditorPane pane = new JEditorPane();
        pane.setContentType("text/html");
        pane.setEditable(false);
        pane.setText("<html><h1>Heading</h1>Text<ul><li>Bullet point</li></ul></html>");

        // add editor pane to frame and set frame visible
        frame.add(pane);
        frame.setVisible(true);
    }
}

P.S.:我现在正在使用

ul {
    list-style-type: none;
    margin-left: 10px
}

在css文件中

<li>&bull; Item 1</li>

在 html 中有一个不错的要点。受 aterai 启发的解决方案。

【问题讨论】:

  • 这可以很好地与text-indent: -20px; padding-left: 20px 之类的内容结合使用,以将文本缩进到项目符号的右侧。不过,我还没有找到如何自动将缩进与项目符号大小对齐。

标签: java html swing rendering jeditorpane


【解决方案1】:

+1 @StanislavL 这是另一个例子(使用 css list-style-image 属性):

bullet.png:

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class HTMLTest2 {
  public JComponent makeEditorPane(String bullet) {
    // create editor pane and fill with some html
    JEditorPane pane = new JEditorPane();
    pane.setContentType("text/html");
    pane.setEditable(false);
    if(bullet!=null) {
      HTMLEditorKit htmlEditorKit = (HTMLEditorKit)pane.getEditorKit();
      StyleSheet styleSheet = htmlEditorKit.getStyleSheet();
      //String u = getClass().getResource(bullet).toString();
      String u = "http://i.stack.imgur.com/jV29K.png";
      styleSheet.addRule(String.format("ul{list-style-image:url(%s);margin:0px 20px;", u));
      //styleSheet.addRule("ul{list-style-type:circle;margin:0px 20px;}");
      //styleSheet.addRule("ul{list-style-type:disc;margin:0px 20px;}");
      //styleSheet.addRule("ul{list-style-type:decimal;margin:0px 20px;}");
    }
    pane.setText("<html><h1>Heading</h1>Text<ul><li>Bullet point</li></ul></html>");
    return pane;
  }
  public JComponent makeUI() {
    JPanel p = new JPanel(new GridLayout(2,1));
    p.add(new JScrollPane(makeEditorPane(null)));
    p.add(new JScrollPane(makeEditorPane("bullet.png")));
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new HTMLTest2().makeUI());
    f.setSize(320, 320);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

【讨论】:

  • 我将此答案标记为解决方案,因为它似乎比 StanislavL 的另一个更容易实现,尽管两者都很好。
  • @aterai 五年过去了,有没有更简单的解决方案,Oracle 有没有办法解决这个问题。
  • @PaulTaylor 在 Swing 组件中的 CSS 支持自 5 年前以来没有改变。
【解决方案2】:

查看 ListView 源代码

public void paint(Graphics g, Shape allocation) {
    super.paint(g, allocation);
    Rectangle alloc = allocation.getBounds();
    Rectangle clip = g.getClipBounds();
    // Since listPainter paints in the insets we have to check for the
    // case where the child is not painted because the paint region is
    // to the left of the child. This assumes the ListPainter paints in
    // the left margin.
    if ((clip.x + clip.width) < (alloc.x + getLeftInset())) {
        Rectangle childRect = alloc;
        alloc = getInsideAllocation(allocation);
        int n = getViewCount();
        int endY = clip.y + clip.height;
        for (int i = 0; i < n; i++) {
        childRect.setBounds(alloc);
        childAllocation(i, childRect);
        if (childRect.y < endY) {
            if ((childRect.y + childRect.height) >= clip.y) {
            listPainter.paint(g, childRect.x, childRect.y,
                      childRect.width, childRect.height,
                      this, i);
            }
        }
        else {
            break;
        }
        }
    }
}

listPainter 使用过去的参数作为

    if (childtype == CSS.Value.SQUARE || childtype == CSS.Value.CIRCLE
        || childtype == CSS.Value.DISC) {
    drawShape(g, childtype, (int) x, (int) y, 
          (int) w, (int) h, align);
    }

drawShape 紧随其后,您可以看到尺寸被硬编码为 8

void drawShape(Graphics g, CSS.Value type, int ax, int ay, int aw, 
               int ah, float align) {
            // Align to bottom of shape.
            int gap = isLeftToRight ? - (bulletgap + 8) : (aw + bulletgap);
            int x = ax + gap;
            int y = Math.max(ay, ay + (int)(align * ah) - 8);

        if (type == CSS.Value.SQUARE) {
        g.drawRect(x, y, 8, 8);
        } else if (type == CSS.Value.CIRCLE) {
        g.drawOval(x, y, 8, 8);
        } else {
        g.fillOval(x, y, 8, 8);
        }
    }

您可以尝试用自己提供的painter提供的方法替换StyleSheet的ListPainter

public ListPainter getListPainter(AttributeSet a) {
    return new ListPainter(a, this);
}

你可以在哪里改变子弹的渲染

【讨论】:

  • 感谢您的搜索。这有效地使所有列表形状在摆动的 html 渲染中变得丑陋。我认为在浏览器中子弹的大小取决于实际的字体大小。
  • 您可以覆盖 ListView 的 'public void paint(Graphics g, Shape allocation)' 方法并在那里提供自己的绘画而不是替换 'ListPainter'
  • 但是为什么默认的这么丑?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-02
  • 1970-01-01
  • 2021-08-24
  • 2011-02-12
  • 1970-01-01
  • 2013-02-24
  • 2016-10-14
相关资源
最近更新 更多