【问题标题】:Problem aligning custom edit field for blackberry为黑莓对齐自定义编辑字段时出现问题
【发布时间】:2011-05-06 15:29:38
【问题描述】:

我使用 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());
        }
    }
}

谢谢。

【问题讨论】:

    标签: java user-interface blackberry layout


    【解决方案1】:

    当您创建 VerticalFieldManager 时,您是在告诉它使用 USE_ALL_WIDTH 吗?如果你不设置那个标志,那么它只会和最宽的组件一样宽,所以无论你为它的孩子设置什么样式,它们看起来总是一样的。

    ==更新==

    好的,要查看的另一件事是您的布局和 getPreferredWidth 方法。在 getPreferredWidth 中,您将其设置为屏幕的宽度。这意味着它将占据管理器的整个宽度,因此无法定位它。尝试根据内容计算实际宽度。与布局方法相同,您使用完整的可用宽度调用 setExtent。这告诉管理器组件应该占据整个宽度。正确计算后,尝试使用首选宽度。

    作为一项快速测试,在您开始尝试计算宽度之前,您可以在其中硬编码一个值。如果这有所不同,那么您可以开始研究如何正确计算宽度。

    【讨论】:

    • 尝试使用 USE_ALL_WIDTH 没有成功。自定义字段包含在 VerticalLayoutManager 中,其他编辑字段似乎正确对齐。这让我觉得我在自定义字段本身中遗漏了一些东西。
    【解决方案2】:

    Dave 给出了很好的答案,代码现在看起来像这样,并且该字段已正确对齐:

    public BorderEditField(String label, String initialValue, int maxNumChars, long style) { super(label, initialValue, maxNumChars, style); }

    private int iRectX = getFont().getAdvance(getLabel());
    
    公共 int getPreferredWidth() {
        return super.getMaxSize() * super.getFont().getAdvance("X");
    }
    
    公共 int getPreferredHeight() {
        返回 super.getPreferredHeight();
    }
    
    公共无效布局(int宽度,int高度){
        宽度 = Math.min( 宽度,getPreferredWidth() );
        高度 = Math.min( 高度,getPreferredHeight() );
        超级布局(宽度,高度);
        设置范围(宽度,高度);
    
    }
    
    公共无效油漆(图形g){
        super.paint(g);
        如果(isFocus()){
            g.setColor(Color.RED);
            g.drawRect(iRectX, 0, getPreferredWidth() , getPreferredHeight());
        } 别的 {
            g.setColor(Color.BLACK);
            g.drawRect(iRectX, 0, getPreferredWidth() , getPreferredHeight());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      相关资源
      最近更新 更多