简体   繁体   中英

Make JLabel html content right-to-left

I want to change a JLabel's content direction to be right to left.
The Jlabel content is HTML and I tested all these (& none of them worked!):

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

How should I do that?

thanks

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

<html><div align=right width=100px>text</div></html>

do you meaning tag dir="rtl"

在此输入图像描述

not directly possible with JLabel or JTextArea , based on BugParade code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM