如何在LWUIT中右对齐TextArea的内容?

发布于 2024-12-18 17:11:01 字数 445 浏览 3 评论 0原文

我想将 TextArea 中的文本向右对齐。我尝试了以下代码:

     Form form = new Form();
     TextArea textArea = new TextArea("Some Arabic text ...");
     textArea.setRTL(true);
     textArea.setAlignment(RIGHT);
     form.addComponent(textArea);


结果只是将滚动条向左移动,
但文本仍然没有对齐RIGHT
检查下图:

在此处输入图像描述

那么如何将内容与 RIGHT 对齐?

I want to align the text in a TextArea to the right. I tried the following code:

     Form form = new Form();
     TextArea textArea = new TextArea("Some Arabic text ...");
     textArea.setRTL(true);
     textArea.setAlignment(RIGHT);
     form.addComponent(textArea);

The result was just moving the scroll to left,
But the text is still not aligned RIGHT,
check the image below:

enter image description here

So how to align the content to the RIGHT ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海之角 2024-12-25 17:11:01

对于第一个实例来说,这可能听起来很疯狂:)但是将对齐设置为 TextArea.LEFT 解决了问题,现在它是 RIGHT 对齐!

    Form form = new Form();
    TextArea textArea = new TextArea("Some Arabic text ...");
    textArea.setRTL(true);
    textArea.setAlignment(TextArea.LEFT);
    form.addComponent(textArea);

将其设置为LEFT 会使显示的文本RIGHT 对齐!

或者通过删除镜像显示的textArea.setRTL(true)

    Form form = new Form();
    TextArea textArea = new TextArea("Some Arabic text ...");
    textArea.setAlignment(TextArea.RIGHT);
    form.addComponent(textArea);


对于那些对更复杂的细节感兴趣的人 > 当设置为 RTL 时:
TextArea 类的 paint 方法是

public void paint(Graphics g) {
    UIManager.getInstance().getLookAndFeel().drawTextArea(g, this);
}

DefaultLookAndFeel 中的 drawTextArea 方法如下:

int align = ta.getAbsoluteAlignment();
// remaining code is here in initial source
switch(align) {
     case Component.RIGHT:
          x = ta.getX() + ta.getWidth() - rightPadding - f.stringWidth(displayText);
          break;
     // remaining code is here in initial source
}
g.drawString(displayText, x, y);

不幸的是,TextArea. RIGHT 值为 3
但是当调用 ta.getAbsoluteAlignment() 时,它会返回 1(尽管对象的对齐方式通过代码设置为 TextArea.RIGHT !!)
同时 TextArea.Left 值为 1
这就是为什么它匹配开关中的值并与 RIGHT 对齐

顺便说一句,如果设置

textArea.setAlignment(Component.RIGHT); 

它也会出错,因为 Component.RIGHT 在绘制方法之外值为 3 而不是 1!

It may sound crazy for the first instance :) but setting the alignment to TextArea.LEFT solved the issue and now it's RIGHT aligned !

    Form form = new Form();
    TextArea textArea = new TextArea("Some Arabic text ...");
    textArea.setRTL(true);
    textArea.setAlignment(TextArea.LEFT);
    form.addComponent(textArea);

Setting it to LEFT makes the displayed text RIGHT aligned !

Or by removing the textArea.setRTL(true) which is mirroring the display

    Form form = new Form();
    TextArea textArea = new TextArea("Some Arabic text ...");
    textArea.setAlignment(TextArea.RIGHT);
    form.addComponent(textArea);


For those who are interested in more complicated details when it's set to RTL:
the paint method of TextArea class is

public void paint(Graphics g) {
    UIManager.getInstance().getLookAndFeel().drawTextArea(g, this);
}

And drawTextArea method in DefaultLookAndFeel is as follows:

int align = ta.getAbsoluteAlignment();
// remaining code is here in initial source
switch(align) {
     case Component.RIGHT:
          x = ta.getX() + ta.getWidth() - rightPadding - f.stringWidth(displayText);
          break;
     // remaining code is here in initial source
}
g.drawString(displayText, x, y);

Unfortunately TextArea.RIGHT value is 3

But when calling ta.getAbsoluteAlignment() it returns 1 (despite that the object's alignment is set by code to TextArea.RIGHT !!)

Meanwhile TextArea.Left value is 1

That's why it matched the value in the switch and was aligned to RIGHT

BTW, if you set

textArea.setAlignment(Component.RIGHT); 

it will also be wrong, because Component.RIGHT outside the paint method has the value 3 not 1 !

凶凌 2024-12-25 17:11:01

您只需编写“TextArea.RIGHT”而不是“RIGHT”

textArea.setAlignment(TextArea.RIGHT);

You only have to write 'TextArea.RIGHT' instead of 'RIGHT'

textArea.setAlignment(TextArea.RIGHT);
耳钉梦 2024-12-25 17:11:01

您可以使用以下行:

TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

You can use the following line:

TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文