我想将TextArea
中的文字对齐到右边。我尝试了以下代码:
Form form = new Form();
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setRTL(true);
textArea.setAlignment(RIGHT);
form.addComponent(textArea);
结果只是将滚动条向左移动,
但文字仍未对齐RIGHT
,
检查下图:
那么如何将内容与RIGHT
对齐?
答案 0 :(得分:2)
对于第一个实例可能听起来很疯狂:)但是将对齐设置为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时:
paint
类的TextArea
方法是
public void paint(Graphics g) {
UIManager.getInstance().getLookAndFeel().drawTextArea(g, this);
}
drawTextArea
中的DefaultLookAndFeel
方法如下:
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
BTW,如果你设置
textArea.setAlignment(Component.RIGHT);
这也是错误的,因为paint方法之外的Component.RIGHT
的值为3而不是1!
答案 1 :(得分:1)
您只需要写'TextArea.RIGHT'而不是'RIGHT'
textArea.setAlignment(TextArea.RIGHT);
答案 2 :(得分:0)
您可以使用以下行:
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);