1

I have a jMenuBar1 and there are two jMenu items (say jMenu1 and jMenu2) added to the jMenuBar1.

I managed to align the jMenuBar1 object with this line of code :

jMenuBar1.add(Box.createHorizontalGlue());    

Now I am trying to align the text in jMenu Items to right and making the text RTL(Right to Left) but nothing seems to work. I have already tried

Menu1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
jMenu1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

and no luck.

any solution will be highly appreciated.

4

1 回答 1

1

您可能需要向我们提供更多信息,也许是一些屏幕截图,说明您所拥有的以及您正在努力实现的目标,这menu.setComponentOrientation()对我来说似乎工作得很好......

我明白了

在此处输入图像描述

运行这个

import javax.swing.*;
import java.awt.*;

public class MenuTest extends JFrame{

    public static void display()
    {
        JFrame frame = new JFrame();
        frame.setSize(400,400);
        frame.setLayout(new GridLayout(6,1));
        JMenuBar menuBar = new JMenuBar();
        menuBar.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        JMenu jMenu1 = new JMenu("Menu1");
        jMenu1.setMinimumSize(new Dimension(200, 20));
        jMenu1.setMaximumSize(new Dimension(200, 20));
        jMenu1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        JMenu jMenu2 = new JMenu("Menu2");
        jMenu2.setMinimumSize(new Dimension(200, 20));
        jMenu2.setMaximumSize(new Dimension(200, 20));
        JMenuItem menuItem1 = new JMenuItem("Item1");
        JMenuItem menuItem2 = new JMenuItem("Item2");
        menuItem1.setMinimumSize(new Dimension(200, 20));
        menuItem1.setMaximumSize(new Dimension(200, 20));
        menuItem1.setPreferredSize(new Dimension(200, 20));
        menuItem1.setSize(new Dimension(200, 20));
        menuItem2.setMinimumSize(new Dimension(200, 20));
        menuItem2.setMaximumSize(new Dimension(200, 20));
        menuItem1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        menuItem2.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        jMenu1.setHorizontalAlignment(JMenu.TRAILING);
        jMenu1.add(menuItem1);
        jMenu1.add(menuItem2);
        menuBar.add(jMenu1);
        menuBar.add(jMenu2);
        menuBar.setSize(frame.getWidth(), 20);
        frame.add(menuBar);
        frame.setVisible(true);
    }
    public static void main(String[] args)
    {
        EventQueue.invokeLater(
                new Runnable() {
                    @Override
                    public void run() {
                        MenuTest.display();
                    }
                }
         );
    }
}
于 2013-06-22T12:44:22.693 回答