我已经使用 JDE4.2.1 编写了一个带边框的自定义编辑字段 然后将该字段添加到 VerticalLayoutManager 并实例化为:
BorderEditField bef = new BorderEditField("Enter a value: ", null, 6,
BorderEditField.FIELD_RIGHT | BorderEditField.FILTER_NUMERIC);
但是,无论我指定哪种样式 (FIELD_HCENTER),字段始终左对齐。 有什么明显的东西我可能会在这里丢失吗?在不同版本的 JDE 上试过,结果相同...
public class BorderEditField extends BasicEditField {
public BorderEditField(String label, String initialValue, int maxNumChars, long style)
{
super(label, initialValue, maxNumChars, style);
}
private int iRectX = getFont().getAdvance(getLabel());
private int iRectWidth = (getMaxSize() * getFont().getAdvance("X")) + 16;
public int getPreferredWidth() {
return Display.getWidth();
}
public void layout(int width, int height) {
super.layout(width, getPreferredHeight());
setExtent(width, getPreferredHeight());
}
public void paint(Graphics g) {
super.paint(g);
if (isFocus()) {
g.setColor(Color.RED);
g.setGlobalAlpha(220);
g.drawRect(iRectX, 0, iRectWidth, getPreferredHeight());
} else {
g.setColor(Color.BLACK);
g.setBackgroundColor(Color.DARKBLUE);
g.setGlobalAlpha(150);
g.drawRect(iRectX, 0, iRectWidth, getPreferredHeight());
}
}
}
谢谢。
最佳答案
当您创建 VerticalFieldManager 时,您是否告诉它使用 USE_ALL_WIDTH?如果您不设置该标志,那么它只会与最宽的组件一样宽,因此无论您为其子项设置什么样式,它们看起来总是一样的。
==更新==
好的,另一件事是您的布局和 getPreferredWidth 方法。在 getPreferredWidth 中,您将其设置为屏幕的宽度。这意味着它将占据管理器的整个宽度,因此无法对其进行定位。尝试根据内容计算实际宽度。与布局方法相同,您使用完整的可用宽度调用 setExtent。这告诉管理器组件应该占据整个宽度。正确计算后,请尝试使用首选宽度。
作为快速测试,在您开始尝试计算宽度的方法之前,您可以在其中硬编码一个值。如果这有所作为,那么您可以开始研究如何正确计算宽度。
关于java - 对齐黑莓的自定义编辑字段时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4123798/