将 JFrame 方向设置为从右到左!
为了从右到左对齐我的 JFrame,我使用:
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
但这仅当我使用以下 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);
}
});
}
}
但我希望它在没有这种装饰的情况下也能工作。如何解决这个问题?
编辑:
@mre 我想要一个像这样的 JFrame:
EDIT2:
我真的非常需要解决这个问题,所以我向谁提供 500+ 提供这样的 JFrame (使用 WindowsLookAndFeel):
To align my JFrame from righ-to-left, I use:
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
but this works only if I use the following style (decoration) of the 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);
}
});
}
}
but I want it to work without this decoration. How to solve this issue?
EDIT:
@mre I want a JFrame like this one:
EDIT2:
I really really need this issue to be fixed, so I offer 500+ to who will give a JFrame like this (with WindowsLookAndFeel):
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
下面解释了您通过代码片段观察到的情况:
ComponentOrientation
仅适用于 Swing(实际上是 AWT)组件。当使用 JFrame.setDefaultLookAndFeelDecolated(true); 时,整个框架装饰由 Swing 执行(实际上是 LookAndFeel 本身),因此这可以按预期工作。
如果您没有设置此选项,则意味着操作系统负责框架装饰,但是操作系统无法知道 Swing 组件使用的
ComponentOrientation
!我希望操作系统(你有没有提到你到底使用什么操作系统?似乎是 Windows 7,对吧?)根据当前选择的区域设置执行正确的装饰。因此,如果您有阿拉伯语言区域设置并在操作系统上选择,我想所有窗口装饰都是从右到左的。您是否尝试更改该区域设置(通过“区域和语言”控制面板)?有效吗?
注意:我认为您不能直接从 Java 更改这些操作系统设置,但您可以使用
Locale.getDefault()
读取它们。总结:
首先,您必须确保您的操作系统在文本方向方面配置正确;抱歉,我在这里帮不了什么忙,因为我的 Windows 计算机上没有安装任何从右到左的语言。
然后,使用系统外观并确保
JFrame.setDefaultLookAndFeelDecolated(false);
如果这不起作用,那么您可以考虑将您的代码片段以及您的系统配置发布到 Oracle Java bug 列表
(
以下是有关如何改进这部分代码的额外说明(但这并不能解决您的问题)
顺便说一句,如果您让用户定义其操作系统语言首选项,那么您不应该显式硬编码
frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
而是使用类似以下内容:
Locale.getDefault()
,除非在应用程序中显式更改,否则将返回当前选择的操作系统级别Locale
。另请注意,最好使用
applyComponentOrientation()
而不是setComponentOrientation()
,因为前者递归地将给定方向设置为组件的整个层次结构。最后,您必须确保在窗口中使用的 LayoutManager 是从右到左感知的;这通常适用于所有标准 Swing 布局,但不适用于所有第 3 方布局管理器(如果您使用某些布局管理器)。
The following explains what you observe through your code snippet:
ComponentOrientation
is applicable only to Swing (and AWT actually) components.When using
JFrame.setDefaultLookAndFeelDecorated(true);
, then the whole frame decoration is performed by Swing (the LookAndFeel itself in fact), so this works as expected.If you don't set this option though, that means that the OS is in charge of the frame decoration, however the OS cannot be aware of the
ComponentOrientation
used by your Swing components!I expect the OS (did you mention what OS you use exactly? It seems to be Windows 7 right?) to perform the correct decoration based on the currently selected Locale. Hence if you have an Arabic locale setup and selected on your OS, I guess all windows decorations are right to left. Did you try changing that Locale (through the "Region and Language" control panel)? Did it work?
Note: I don't think that you can change these OS settings directly from Java, but you can read them with
Locale.getDefault()
.To sum up:
first of all, you have to ensure that your OS is properly configured in terms of text orientation; sorry I can't help much here because I don't have any right-to-left languages installed on my Windows machine.
then, use the system look and feel and ensure that
JFrame.setDefaultLookAndFeelDecorated(false);
if that doesn't work, then you may consider posting your code snippet, along with your system configuration to Oracle Java bugs list
What follows are extra notes on how to improve this part of your code (but this is not a fix for your issue)
By the way, if you let the user define its OS language preferences, then you shouldn't explicitly hard-code
frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
but rather use something like:Where
Locale.getDefault()
, unless explicitly changed within your application, will return the OS-level currently selectedLocale
.Also note that it is preferable to use
applyComponentOrientation()
rather thansetComponentOrientation()
, because the former recursively sets the given orientation to the whole hierarchy of components.Finally, you will have to ensure in your windows that the
LayoutManager
used is right-to-left aware; this is normally OK with all standard Swing layouts, but not for all 3rd-party layout managers, if you use some.@Eng.Fouad
只是开玩笑,这个???...
代码:
@Eng.Fouad
just joke and this one ???...
code:
我怀疑它必须与操作系统做更多的事情。通常(如果您不调用 setDefaultLookAndFeelDecolated),是操作系统提供框架装饰,而不是 LAF。
您应该尝试更改操作系统中的首选项,以表示您想要从右到左的方向。
抱歉,我不知道这些设置在哪里。
完成此操作后,您应该能够删除对 setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 的调用。
因为 LAF 将从区域设置中获取操作系统设置。
编辑
此链接介绍了如何启用Windows 7 上从右到左的文本。然后我认为您还需要更改您的区域设置。
I suspect it has to do more with the OS. Normally (if you don't call
setDefaultLookAndFeelDecorated
) it is the OS that provides the frame decoration, not the LAF.You should try changing your preferences in the OS to say you want right to left orientation.
Sorry, I don't know where those settings would be.
Once you do this, then you should be able to remove the call to
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
as the LAF will pick up the OS settings from the Locale.EDIT
This Link describes how to enable right-to-left text on Windows 7. Then I think you would also need to change your locale.
Windows LookAndFeel 似乎不支持组件方向功能(至少标题栏不支持)
It looks like the Component Orientation feature is not supported with the Windows LookAndFeel (at least not for the title bar)
这是 一种可能性。该实用程序是为已切换到 Windows 并希望窗口按钮位于左侧的 Mac 用户设计的,但它应该能够满足与您相同的需求。
这个解决方案与 Java 无关(所以我不知道它是否可以满足您的需求)并且听起来它对于您的应用程序来说是外部的。我自己无法尝试(我没有运行 Windows),所以我不能保证它,但它可能值得一试。
Here is one posibility. This utility is designed for Mac users who have switched to Windows and want the window buttons on the left, but it should serve the same needs as yours.
This solution has nothing to do with Java (so I don't know if it's even acceptable for your needs) and sounds like it would be external to your application. I have not been able to try it out myself (I'm not running Windows), so I can't vouch for it, but it might be worth a try.
你只需要添加这行代码,我确信它可以 100% 工作。
You only need to add this line of code and I am sure it works 100%.