如果 Text-Not-Fitting (JDK 7 到 18),JTextField 呈现错误

JTextField render bug if Text-Not-Fitting (JDK 7 till 18)

JTextField 在 RIGHT_TO_LEFT 组件方向上呈现错误的文本

我们需要一个 解决方法,因为每当 JTextField 填充包含以下内容的文本时:

然后 JTextField 在意想不到的位置呈现文本部分。

(It only renders right if the complete text Fits inside the JTextField.)

我们用来重现的文字是:

其他信息:


可执行测试用例的源代码:

//
// Like you may notice, below code shows simple JTextField, 
// but once you resize the Window smaller than the text Fits,
// then you experience numbers dancing (moving around randomly).
//
// And trying to select parts of text is even more fatal (random parts are rendered).
//
package test;

import java.awt.ComponentOrientation;

public class JavaBug extends javax.swing.JFrame {

    public static void main(String[] args) {
        JavaBug frame = new JavaBug();
        frame.show();
    }
    
    public JavaBug() {
        javax.swing.JTextField textField = new javax.swing.JTextField();
        
        textField.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        
        // Below is just name of a product, added inside an accounting software.
        textField.setText("\u0635\u0646\u062F\u0648\u0642 \u06F4\u06F0×\u06F3\u06F0 \u067E\u0627\u06CC\u0647 \u062F\u0627\u0631 \u0648\u0627\u06CC\u0631\u0646\u06AF \u0645\u06CC\u062A\u0631 \u062A\u06A9 \u0641\u0627\u0632");
        textField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        
        getContentPane().add(textField);
        pack();
        this.setLocationRelativeTo(null); //enusre get showed at screen center
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    }
}

屏幕截图

只是 运行 以上代码结果为:

调整大小后,减少一些:


开发工具包或运行时版本:

你可以试试这个

import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;

public class JavaBug extends javax.swing.JFrame {

    public static void main(String[] args) {
        JavaBug frame = new JavaBug();
        frame.show();
    }

    public JavaBug() {
        JTextPane textPane = new JTextPane();
        textPane.setEditorKit(new HTMLEditorKit());
        textPane.setText("<html><font size='+2'>\u0635\u0646\u062F\u0648\u0642 \u06F4\u06F0×\u06F3\u06F0 \u067E\u0627\u06CC\u0647 \u062F\u0627\u0631 \u0648\u0627\u06CC\u0631\u0646\u06AF \u0645\u06CC\u062A\u0631 \u062A\u06A9 \u0641\u0627\u0632</font></html>");
        textPane.getDocument().putProperty("i18n", Boolean.TRUE);
        JPanel noWrapPanel = new JPanel( new BorderLayout() );
        noWrapPanel.add( textPane );
        JScrollPane scrollPane = new JScrollPane( noWrapPanel );
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        /* without below right to left force there is an other bug when we press home and
         * try to navigate to end using left key it will never get at end and fall back at start. kind of crazy bug */
        java.util.Locale arabic = new java.util.Locale("ar", "KW");
        ComponentOrientation arabicOrientation = ComponentOrientation.getOrientation(arabic);
        textPane.applyComponentOrientation(arabicOrientation);

        getContentPane().add(scrollPane);
        pack();
        this.setLocationRelativeTo(null); //enusre get showed at screen center
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    }
}