【问题标题】:Java Display Text On Right Side of Text AreaJava 在文本区域右侧显示文本
【发布时间】:2012-03-11 21:38:14
【问题描述】:

ComponentOrientation.RIGHT_TO_LEFT 的问题是诸如“/”或“!”之类的字符或者 '。'显示在左侧。

我只需要从右到左绘制文本,但对文本字符使用标准的西方英语从左到右表示法。

如果不手动渲染文本,这可能吗?

【问题讨论】:

    标签: java swing text orientation right-to-left


    【解决方案1】:

    我无法生成您的问题,请您使用我的SSCCE 以显示在左侧。

    来自代码

    import java.awt.ComponentOrientation;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.UIManager;
    
    public class RightToLeft {
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    JTextArea text = new JTextArea(10, 5);
                    text.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                    text.setText("one/\n "
                            + "!two\n"
                            + ".three\n"
                            + "/four\n"
                            + "five!\n"
                            + "six.\n"
                            + "seven\n"
                            + "eight\n");
                    JScrollPane pane = new JScrollPane(text);
                    JFrame.setDefaultLookAndFeelDecorated(true);
                    JFrame frame = new JFrame("العنوان بالعربي");
                    frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                    frame.add(pane);
                    frame.pack();
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }
    

    【讨论】:

    • OP 希望像 "one/" 这样的文本显示为右对齐,但布局为从左到右。
    • 我使用 JTextField.setHorizo​​ntalAlignment(JTextField.RIGHT) 让它工作
    【解决方案2】:

    尝试为文本字段调用setAlignmentX(Component.RIGHT_ALIGNMENT)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      相关资源
      最近更新 更多