我找到的最佳解决方案是更改getAsText(),使其返回的不是值+单位,而是值。与setAsText相同。这样我就可以使用默认的InplaceEditor,让单位在视图中消失。
为了让单位显示在非选中的视图中,我必须重写isPaintable和paintValue方法,如下所示:
@Override
public boolean isPaintable() {
return true;
}
/**
* Draws the number and unit in the rectangular region of the screen given
* to this PropertyEditor.
* @param gfx
* @param box
*/
@Override
public void paintValue(Graphics gfx, Rectangle box) {
Color oldColor = gfx.getColor();
gfx.setColor(isEditable() ? EDITABLE_COLOR : NON_EDITABLE_COLOR);
// drawString takes x, y of lower left corner of string, whereas the box
// x, y is at the top left corner of the string; need to add to translate
// to where the string should be drawn
gfx.drawString(getAsText() + " " + getViewUnit(),
box.x + LEFT_MARGIN_PIXELS, box.y + LINE_HEIGHT_PIXELS);
gfx.setColor(oldColor);
}
注意,LEFT\u MARGIN\u PIXELS=0,以便使文本与默认属性编辑器对齐;线条高度像素是15。