JTextPane插入组件,垂直对齐错误 [英] JTextPane insert component, faulty vertical alignment

查看:135
本文介绍了JTextPane插入组件,垂直对齐错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTextPane,我需要在其中插入JComponent.我正在使用

I have a JTextPane, into which I need to insert a JComponent. I'm using

JTextPane.insertComponent(Component)

确实插入了该项目,但是垂直位置太高.该组件不是使组件的底部与当前文本行的基线对齐,而是位于该位置的上方,从而阻止了出现在上面的文本行/过度绘制了这些行.

The item is indeed inserted, but the vertical positioning is too high. Instead of having the bottom of the component aligned with the baseline of the current line of text, the component is way above that position, blocking out/over-painting lines of text appearing above.

我尝试在插入的组件和JTextPane上调用具有各种值的setAlignmentY(float),但它根本不影响行为.

I have tried calling setAlignmentY(float) with various values, on both the inserted component and the JTextPane, but it doesn't affect the behavior at all.

我的猜测:我的JTextPane或其文档中似乎存在一些需要更改的状态.但是我不知道这是什么.

My guess: there seems to be some state inside my JTextPane or its Document that I need to be changing. But I don't know what it is.

推荐答案

我知道,这是一个非常老的问题,但是您使用setAlignmentY的方法是完全正确的.不知道是什么代码导致它无法正常工作,但是JTextPane.insertComponent(Component)的javadoc对对齐进行了以下说明:

I know, this is a pretty old question, but your approach of using setAlignmentY is totally correct. Don't know what code caused it to not work, but the javadoc of JTextPane.insertComponent(Component) says the following about the alignment:

根据 Component.getAlignmentY返回的值.对于Swing组件,这 值可以使用以下方法方便地设置 JComponent.setAlignmentY.例如,将值设置为0.75将 导致75%的组件高于基线,而25% 低于基准线的百分比

The component is placed relative to the text baseline according to the value returned by Component.getAlignmentY. For Swing components this value can be conveniently set using the method JComponent.setAlignmentY. For example, setting a value of 0.75 will cause 75 percent of the component to be above the baseline, and 25 percent of the component to be below the baseline

因此,使用textPane.setAlignmentY(1.0f)具有预期的效果.

So using textPane.setAlignmentY(1.0f) has the desired effect.

这篇关于JTextPane插入组件,垂直对齐错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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