从右到左设置JFrame Orientation!

Eng*_*uad 15 java swing arabic jframe orientation

为了将我的JFrame从左到右对齐,我使用:

setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
Run Code Online (Sandbox Code Playgroud)

但这只有在我使用JFrame的以下样式(装饰)时才有效:

public class RightToLeft {
  public static void main(String []args){
    javax.swing.SwingUtilities.invokeLater(new Runnable(){
      public void run() {
        try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); }
        catch (Exception e) { e.printStackTrace(); }
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("??????? ???????");
        frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
      }
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但我希望它没有这种装饰.如何解决这个问题?

编辑:

@mre我想要一个像这样的JFrame:

在此输入图像描述

EDIT2:

我真的需要修复这个问题,所以我提供500+给谁一个像这样的JFrame(使用WindowsLookAndFeel):

在此输入图像描述

jfp*_*ret 7

以下内容解释了您通过代码段观察到的内容:

ComponentOrientation 仅适用于Swing(和实际的AWT)组件.

使用时JFrame.setDefaultLookAndFeelDecorated(true);,整个框架装饰由Swing(实际上是LookAndFeel本身)执行,因此这可以按预期工作.

如果你没有设置这个选项,这意味着操作系统负责框架装饰,但操作系统无法知道ComponentOrientation你的Swing组件使用的!

我希望操作系统(您是否提到了您使用的操作系统?看起来似乎是Windows 7吗?)根据当前选择的区域设置执行正确的装饰.因此,如果您在操作系统上设置并选择了阿拉伯语区域设置,我猜所有的窗户装饰都是从右到左.您是否尝试更改该区域设置(通过"区域和语言"控制面板)?它有用吗?

注意:我认为您不能直接从Java更改这些操作系统设置,但您可以使用它来阅读它们Locale.getDefault().

总结一下:

  • 首先,您必须确保您的操作系统在文本方向上正确配置 ; 对不起,我在这里帮不了多,因为我的Windows机器上没有安装任何从右到左的语言.

  • 然后,使用系统的外观和感觉,并确保 JFrame.setDefaultLookAndFeelDecorated(false);

  • 如果这不起作用,那么您可以考虑将您的代码片段以及您的系统配置发布到Oracle Java错误列表中

以下是关于如何改进这部分代码的额外说明(但这不是解决您的问题的方法)

顺便说一句,如果你让用户定义它的操作系统语言首选项,那么你不应该明确硬编码frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);,而是使用类似的东西:

frame.applyComponentOrientation(
    ComponentOrientation.getOrientation(Locale.getDefault()));
Run Code Online (Sandbox Code Playgroud)

其中Locale.getDefault(),除非你的应用程序中明确改变,将返回当前选择的操作系统级Locale.

还要注意,最好使用applyComponentOrientation()而不是setComponentOrientation(),因为前者递归地将给定方向设置为组件的整个层次结构.

最后,您必须在窗口中确保LayoutManager使用的是从右到左的意识; 这通常适用于所有标准Swing布局,但如果使用某些布局管理器,则不适用于所有第三方布局管理器.


mKo*_*bel 5

@ Eng.Fouad

只是开玩笑,这一个??? ...

物质L&F

码:

import java.awt.ComponentOrientation;
import javax.swing.JFrame;
import javax.swing.UIManager;
import org.pushingpixels.substance.api.skin.SubstanceOfficeSilver2007LookAndFeel;

public class RightToLeft {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                    UIManager.setLookAndFeel(new SubstanceOfficeSilver2007LookAndFeel());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                JFrame.setDefaultLookAndFeelDecorated(true);
                JFrame frame = new JFrame("??????? ???????");
                frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                frame.setSize(300, 300);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }

    private RightToLeft() {
    }
}
Run Code Online (Sandbox Code Playgroud)