java 从右到左设置 JFrame 方向!
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6455831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set JFrame Orientation from right to left!
提问by Eng.Fouad
To align my JFrame from righ-to-left, I use:
要从右到左对齐我的 JFrame,我使用:
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
but this works only if I use the following style (decoration) of the JFrame:
但这仅在我使用 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:
@mre 我想要一个这样的 JFrame:
EDIT2:
编辑2:
I really really need this issue to be fixed, so I offer 500+ to who will give a JFrame like this (with WindowsLookAndFeel):
我真的真的需要解决这个问题,所以我提供 500+ 给谁将提供这样的 JFrame(使用 WindowsLookAndFeel):
采纳答案by mKorbel
@Eng.Fouad
@Eng.Fouad
just joke and this one ???...
开个玩笑,还有这个???...
code:
代码:
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() {
}
}
回答by jfpoilpret
The following explains what you observe through your code snippet:
下面解释了您通过代码片段观察到的内容:
ComponentOrientation
is applicable only to Swing (and AWT actually) components.
ComponentOrientation
仅适用于 Swing(实际上是 AWT)组件。
When using JFrame.setDefaultLookAndFeelDecorated(true);
, then the whole frame decoration is performed by Swing (the LookAndFeel itself in fact), so this works as expected.
使用时JFrame.setDefaultLookAndFeelDecorated(true);
,整个框架装饰由 Swing 执行(实际上是 LookAndFeel 本身),因此这按预期工作。
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!
但是,如果您不设置此选项,则意味着操作系统负责框架装饰,但是操作系统无法知道ComponentOrientation
您的 Swing 组件使用的 !
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?
我希望操作系统(你有没有提到你使用的操作系统是什么?它似乎是 Windows 7 吧?)根据当前选择的Locale执行正确的装饰。因此,如果您在操作系统上设置并选择了阿拉伯语语言环境,我猜所有窗口装饰都是从右到左的。您是否尝试更改该区域设置(通过“区域和语言”控制面板)?它起作用了吗?
Note: I don't think that you can change these OS settings directly from Java, but you can read them with Locale.getDefault()
.
注意:我认为您不能直接从 Java 更改这些操作系统设置,但您可以使用Locale.getDefault()
.
To sum up:
总结:
first of all, you have to ensure that your OS is properly configuredin 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
首先,您必须确保您的操作系统在文本方向方面进行了正确配置;抱歉,我在这里帮不了什么忙,因为我的 Windows 机器上没有安装任何从右到左的语言。
然后,使用系统外观并确保
JFrame.setDefaultLookAndFeelDecorated(false);
如果这不起作用,那么您可以考虑将您的代码片段以及您的系统配置发布到 Oracle Java 错误列表
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:
顺便说一句,如果您让用户定义其操作系统语言首选项,那么您不应该明确地进行硬编码frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
,而应该使用以下内容:
frame.applyComponentOrientation(
ComponentOrientation.getOrientation(Locale.getDefault()));
Where Locale.getDefault()
, unless explicitly changed within your application, will return the OS-level currently selected Locale
.
其中Locale.getDefault()
,除非你的应用程序中明确改变,将返回当前选择的操作系统级Locale
。
Also note that it is preferable to use applyComponentOrientation()
rather than setComponentOrientation()
, because the former recursively sets the given orientation to the whole hierarchy of components.
另请注意,最好使用applyComponentOrientation()
而不是setComponentOrientation()
,因为前者递归地将给定的方向设置为整个组件层次结构。
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.
最后,您必须确保在您的窗口中LayoutManager
使用的是从右到左的感知;这通常适用于所有标准 Swing 布局,但不适用于所有 3rd 方布局管理器,如果您使用一些。
回答by wolfcastle
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.
我怀疑它与操作系统有更多关系。通常(如果您不调用setDefaultLookAndFeelDecorated
)是操作系统提供框架装饰,而不是 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.
抱歉,我不知道这些设置在哪里。执行此操作后,您应该能够删除对 的调用,setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
因为 LAF 将从区域设置中获取操作系统设置。
EDIT
编辑
This Linkdescribes how to enable right-to-left text on Windows 7. Then I thinkyou would also need to change your locale.
回答by wolfcastle
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.
这是一种可能性。此实用程序专为已切换到 Windows 并希望使用左侧窗口按钮的 Mac 用户设计,但它应该满足与您相同的需求。
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.
该解决方案与 Java 无关(所以我不知道它是否可以满足您的需求)并且听起来像是您的应用程序的外部。我自己还没有尝试过(我没有运行 Windows),所以我不能保证它,但它可能值得一试。
回答by javagruffian
It looks like the Component Orientation feature is not supported with the Windows LookAndFeel (at least not for the title bar)
看起来 Windows LookAndFeel 不支持组件方向功能(至少不支持标题栏)