gpt4 book ai didi

java - 如果文本不适合,则 JTextField 呈现错误(JDK 7 到 18)

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

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

我们需要一个解决方法,因为无论何时 JTextField填充的文本包含:

  • Right to left text比如阿拉伯语,
  • Latin numbers ,
  • Latin text .

然后JTextField在意外位置呈现文本部分。

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

我们用来重现的文本是:

  • صندوق ۴۰×۳۰ پایه دار وایرنگ میتر تک فاز

其他信息:

  • 以上文字只是产品名称,添加到我们创建的会计软件中,但我们的用户匹配的渲染失败数量超出了我们的重现范围。
  • 我们的报告地址:JDK-8194733
  • 我们没有使用 Java 自定义渲染来解决问题的经验,但渲染整个文本而不考虑限制并对其进行剪辑应该可以解决问题。
<小时/>

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

//
// 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);
}
}

屏幕截图

只需运行上面的代码结果即可:
just run

调整大小后,减少一些:
after resize

<小时/>

开发套件或运行时版本:

  • Java 版本“1.8.0_25”
  • Java(TM) SE 运行时环境(版本 1.8.0_25-b18)
  • Java HotSpot(TM) 客户端虚拟机(版本 25.25-b02,混合模式)

最佳答案

你可以试试这个

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);
}
}

关于java - 如果文本不适合,则 JTextField 呈现错误(JDK 7 到 18),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48203021/

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