我需要从“右到左”在框架上显示对象吗?





C#中的相同程序
那就是我想要的物体看起来像



请帮我 :)
谢谢

最佳答案

尝试通过对要在其中使用阿拉伯语的每个组件调用setComponentOrientation()方法来更改组件方向,对于组合框,您应覆盖其单元格渲染器以支持从右到左显示,请尝试以下代码:

public class Test extends JFrame
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable(){
            public void run()
            {
                try
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                }
                catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e)
                {
                    e.printStackTrace();
                }
                new Test();
            }
        });
    }

    public Test()
    {
        setBounds(100,100,380,400);
        setTitle("بوابة العالم");
        getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
        JComboBox<String> comboBox = new JComboBox<String>(new String[]{"التاريخ","الفلسفة","الفلك"});
        comboBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        comboBox.setPreferredSize(new Dimension(300,comboBox.getPreferredSize().height));
        comboBox.setRenderer(new CellRenderer());
        add(comboBox);

        JTextPane textPane = new JTextPane();
        textPane.setPreferredSize(new Dimension(300, 300));
        textPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        JScrollPane scroll = new JScrollPane(textPane);
        scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        add(scroll);

        setVisible(true);
    }

}

class CellRenderer implements ListCellRenderer<String>
{
    @Override
    public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus)
    {
        JLabel label = new JLabel(value);
        label.setOpaque(true);
        label.setFocusable(true);
        label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        if (isSelected) {
            label.setBackground(list.getSelectionBackground());
            label.setForeground(list.getSelectionForeground());
        } else {
            label.setBackground(list.getBackground());
            label.setForeground(list.getForeground());
        }
        return label;
    }
}


结果:



对于标题栏方向,如果您使用标题栏的宿主外观,则不适用于您,但如果您调用JFrame.setDefaultLookAndFeelDecorated(true),它将起作用,然后您的应用将使用所提供的外观,而不是宿主的外观。应该在框架上调用setComponentOrientation()方法以指定所需的方向,请尝试使用以下代码从右向左激活标题栏:

public class Test extends JFrame
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable(){
            public void run()
            {
                JFrame.setDefaultLookAndFeelDecorated(true);
                new Test();
            }
        });
    }

    public Test()
    {
        setBounds(100,100,380,400);
        setTitle("بوابة العالم");
        setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
        JComboBox<String> comboBox = new JComboBox<String>(new String[]{"التاريخ","الفلسفة","الفلك"});
        comboBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        comboBox.setPreferredSize(new Dimension(300,comboBox.getPreferredSize().height));
        comboBox.setRenderer(new CellRenderer());
        add(comboBox);

        JTextPane textPane = new JTextPane();
        textPane.setPreferredSize(new Dimension(300, 300));
        textPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        JScrollPane scroll = new JScrollPane(textPane);
        scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        add(scroll);

        setVisible(true);
    }

}

class CellRenderer implements ListCellRenderer<String>
{
    @Override
    public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus)
    {
        JLabel label = new JLabel(value);
        label.setOpaque(true);
        label.setFocusable(true);
        label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        if (isSelected) {
            label.setBackground(list.getSelectionBackground());
            label.setForeground(list.getSelectionForeground());
        } else {
            label.setBackground(list.getBackground());
            label.setForeground(list.getForeground());
        }
        return label;
    }
}


结果:



注意:


不要忘记使用UTF-8编码保存源文件以支持阿拉伯字符。
在创建任何框架实例之前,必须先调用JFrame.setDefaultLookAndFeelDecorated(true)

09-11 11:35