3

我想将 JLabel 的内容方向更改为从右到左。
Jlabel 内容是 HTML,我测试了所有这些(并且没有一个有效!):

    lbl.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    lbl.setHorizontalTextPosition(JLabel.RIGHT);
    lbl.setHorizontalAlignment(JLabel.RIGHT);

我该怎么做?

谢谢

4

2 回答 2

5

使用div元素设置文本的对齐方式和宽度:

<html><div align=right width=100px>text</div></html>
于 2012-07-25T20:35:24.843 回答
3

你的意思是标签吗dir="rtl"

在此处输入图像描述

不能直接使用JLabelor JTextArea,基于BugParade 代码

import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
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();
                }
                String str =("<html > <head>  <title>Test</title> <style> </style> </head> <body> <div align=right>"
                        + " <p><b><font face=Arial color=#5078A8><span lang=he>Hebrew Test</span></font></b></p> "
                        + "<p><span lang=he><font face=Arial size=2> HTML Table</font></span></p> <table dir =rtl "
                        + "border=1 cellpadding=10 width=62% id=AutoNumber1 bordercolorlight=#FFFFFF bordercolordark=#FFFFFF> "
                        + "<tr> <td width=28% valign=top bgcolor=#D0E4F8 class=cellclass><font face=Arial size=2 "
                        + "color=#687C98><b>1 2 3</b></font></td><td width=72% valign=top bgcolor=#D0E4F8 dir=rtl> "
                        + "<font face=Arial size=2><span lang=he>2</span></font></td> </tr> <tr> "
                        + "<td width=28% valign=top bgcolor=#F8F8FF style=color #687C98><b> <font face=Arial size=2>"
                        + "<span lang=he>3</span></font></b></td> <td width=72% align=right valign=top bgcolor=#F8F8FF> "
                        + "<font face=Arial size=2><span lang=he>4</span></font></td> </tr> <tr> "
                        + "<td width=28% valign=top bgcolor=#D0E4F8> <font face=Arial size=2 color=#687C98><b>5</b>"
                        + "</font></td> <td width=72% align=right valign=top bgcolor=#D0E4F8> <font face=Arial size=2>"
                        + "<span lang=he>6 </span></font></td> </tr> <tr> <td width=28%>LEFT&nbsp</td> "
                        + "<td width=72%>RIGHT&nbsp</td> </tr> </table> </div> </body> </html> ");
                JTextPane text = new JTextPane();
                text.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                text.setContentType("text/html");
                text.setText(str);
                JScrollPane pane = new JScrollPane(text);
                JEditorPane text1 = new JEditorPane();
                text1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                text1.setContentType("text/html");
                text1.setText(str);
                JScrollPane pane1 = new JScrollPane(text1);
                JLabel text2 = new JLabel();
                text2.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                //text2.setContentType("text/html"); // not implemented
                text2.setText(str);
                JScrollPane pane2 = new JScrollPane(text2);
                JTextArea text3 = new JTextArea();
                text3.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                //text3.setContentType("text/html"); // not implemented
                text3.setText(str);
                JScrollPane pane3 = new JScrollPane(text3);
                JFrame.setDefaultLookAndFeelDecorated(true);
                JFrame frame = new JFrame("العنوان بالعربي");
                frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                frame.setLayout(new GridLayout(2,2,5,5));
                frame.add(pane);
                frame.add(pane1);
                frame.add(pane2);
                frame.add(pane3);
                //frame.pack();
                frame.setSize(400,600);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}
于 2012-07-25T21:02:39.137 回答