gpt4 book ai didi

user-interface - Blackberry - 在自定义 ButtonField 的中心绘制标签?

转载 作者:行者123 更新时间:2023-12-02 00:43:48 24 4
gpt4 key购买 nike

我在黑莓中创建了一个 CustomButtonField,我可以使用它来设置我们自己的按钮高度和宽度。我面临的问题是我不知道如何在按钮的中心显示标签。

最佳答案

您可以使用paint方法修改标签布局、颜色和字体。
alt text http://img9.imageshack.us/img9/8017/custombutton.jpg
参见示例:

class CustomButton extends ButtonField {
int mHeight;
int mWidth;

public CustomButton(int height, int width, String label) {
super(label, CONSUME_CLICK);
mHeight = height;
mWidth = width;
}

public int getPreferredHeight() {
return mHeight;
}

public int getPreferredWidth() {
return mWidth;
}

protected void layout(int width, int height) {
super.layout(mWidth, mHeight);
setExtent(mWidth, mHeight);
}

protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
String label = getLabel();
int x = (getPreferredWidth() - getFont().getAdvance(label)) >> 1;
int y = (getPreferredHeight() - getFont().getHeight()) >> 1;
graphics.drawText(label, x, y);
}
}

使用示例:

class Scr extends MainScreen implements FieldChangeListener {
CustomButton button1;
CustomButton button2;
CustomButton button3;

public Scr() {
add(button1 = new CustomButton(15, 60, "first"));
button1.setChangeListener(this);
add(button2 = new CustomButton(30, 120, "second"));
button2.setChangeListener(this);
add(button3 = new CustomButton(50, 200, "third"));
button3.setChangeListener(this);
}

public void fieldChanged(Field field, int context) {
if (field == button1)
Dialog.inform("first");
if (field == button2)
Dialog.inform("second");
if (field == button3)
Dialog.inform("third");
}

}

关于user-interface - Blackberry - 在自定义 ButtonField 的中心绘制标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1623307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com