You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX TextArea设为RIGHT_TO_LEFT时追加文本异常求助

Fixing Right-to-Left TextArea Decimal Dot Placement in JavaFX Calculator

I’ve run into this exact quirk with JavaFX’s TextArea when using RIGHT_TO_LEFT orientation before — it’s not exactly a bug, but a side effect of how bidirectional text handling works in JavaFX. Punctuation like the decimal dot gets treated as neutral text, so in RTL mode it ends up at the logical start of the string (which looks like the visual left), while numbers are strong left-to-right (LTR) characters that stay in their typed order. That’s why you see .98 instead of 98. after pressing the dot.

Here are two straightforward fixes that work perfectly:

1. Replace appendText() with manual text concatenation

The appendText() method respects the text node’s orientation when adding content, which causes the dot to jump to the start. Instead, we can grab the current text, append the new character manually, and set the full string back with setText() — this preserves the logical order we want, while keeping the visual right alignment.

Modify your controller’s appenToResult method like this:

private void appenToResult(String ch) {
    String currentContent = textArea.getText();
    textArea.setText(currentContent + ch);
}

With this change, pressing 9 → 8 → . will show 98. immediately, and adding 7 will update to 98.7 as expected.

2. Switch to a Label for single-line display (simpler option)

If your calculator only needs to display a single line of results (which most do), a Label is actually a better fit than TextArea. You can set its alignment to right, which gives the same visual effect without dealing with RTL text orientation quirks.

Update your FXML to replace the TextArea with a Label:

<Label fx:id="resultLabel" layoutX="15.0" layoutY="14.0" 
       prefHeight="66.0" prefWidth="221.0" alignment="CENTER_RIGHT"
       style="-fx-background-color: white;">
    <font>
        <Font size="35.0" />
    </font>
</Label>

Then adjust your controller code to use the Label instead:

@FXML private Label resultLabel;

private void appenToResult(String ch){
    resultLabel.setText(resultLabel.getText() + ch);
}

This approach avoids the RTL text handling entirely while maintaining the right-aligned display you want.

内容的提问来源于stack exchange,提问作者Alexandre Krabbe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:27:47