简体   繁体   English

Java ImageView获取位置并设置对齐方式

[英]Java ImageView get position and set alignment

http://docs.oracle.com/javase/6/docs/api/javax/swing/text/html/ImageView.html http://docs.oracle.com/javase/6/docs/api/javax/swing/text/html/ImageView.html

JEditorPane contains within itself the ImageView. JEdi​​torPane本身包含ImageView。 I can get the size and alignment: 我可以得到大小和对齐方式:

    ImageView.getPreferredSpan(View.X_AXIS)); //20px
    ImageView.getPreferredSpan(View.Y_AXIS)); //20px
    ImageView.getAlignment(View.X_AXIS)); //0.5
    ImageView.getAlignment(View.Y_AXIS)); //1.0

I need to get the coordinates x/y (absolute or relative) and change the y alignment to 0.75. 我需要获取坐标x / y(绝对或相对)并将y对齐更改为0.75。 How can I do this? 我怎样才能做到这一点?

For reference, the value derives from the align attribute of the img tag, as defined in the applicable HTML 3.2 Reference Specification . 作为参考,该值来自img标签的align属性,如适用的HTML 3.2参考规范中所定义。 Values other than left (0.0), middle (0.5) or right (1.0) are not supported directly. 不直接支持 (0.0), (0.5)或 (1.0)以外的值。

A bit late, but I ran into the same problem. 有点晚了,但是我遇到了同样的问题。 The only way to fix this, or so I did it, was, to create your own ImageView and overwrite the vAlign value in its setPropertiesFromAttributes() function. 解决这个问题的唯一方法,或者说我做到了,是创建自己的ImageView并覆盖其setPropertiesFromAttributes()函数中的vAlign值。 Problem is, that setPropertiesFromAttributes is protected, so you have to copy and paste the whole original code of ImageView into your own, and then replace the code to: 问题是setPropertiesFromAttributes受保护,因此您必须将ImageView的整个原始代码复制并粘贴到自己的代码中,然后将代码替换为:

    vAlign = 1.0f;
    if (alignment != null) {
        alignment = alignment.toString();
        if ("top".equals(alignment)) {
            vAlign = 0f;
        }
        else if ("middle".equals(alignment)) {
            vAlign = .5f;
        }
    }
    vAlign = .745f;

You also need to create your own HTMLEditorKit and overwrite getViewFactory(): 您还需要创建自己的HTMLEditorKit并覆盖getViewFactory():

@Override
    public ViewFactory getViewFactory() {

        return new HTMLEditorKit.HTMLFactory() {

            public View create(Element e) {

                View v = super.create(e);

                Object o = e.getAttributes().getAttribute(StyleConstants.NameAttribute);

                if (o instanceof HTML.Tag) {

                    HTML.Tag kind = (HTML.Tag) o;

                    if (kind == HTML.Tag.IMG) {
                        return new MyImageView(e);
                    }
                }
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM