gpt4 book ai didi

java - 如何为 BoxLayout 创建垂直 javax.swing.ComponentOrientation?

转载 作者:太空宇宙 更新时间:2023-11-04 14:34:58 25 4
gpt4 key购买 nike

我正在编写 BoxLayout 的替代方案,因此我需要测试 PAGE_AXIS 和 LINE_AXIS。这需要在 HORIZ_BIT 关闭的情况下创建 ComponentOrientation 值。好像没有这种动物。

如何为垂直布局而不是水平布局创建 ComponentOrientation?(还有一个附属问题:BoxLayout 是否经过彻底测试?)

最佳答案

我第一次尝试利用序列化来解决该问题。这是巨大的杀伤力。该任务可以通过反射来完成,反射允许调用私有(private)构造函数。

// from ComponentOrientation.java
private static final int HORIZ_BIT = 2;
private static final int LTR_BIT = 4;

/**
* Generate the full gamut of ComponentOrientation values.
*
* ComponentOrientation mentions various languages that write
* vertically, but offers no way to construct vertical orientations.
* The only un-deprecated way to get a ComponentOrientation
* value is by referring to the Locale, but ComponentOrientation
* understands only a few locales, none of them with vertical text.
*
* See also http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4418738
* which describes the rationale behind lobotomizing Locale handling
* in ComponentOrientation.
*
* An inability to generate vertical orientations stymies Blox
* which relies on the orientation to resolve PAGE_AXIS and LINE_AXIS.
*
* @param ltr Should text lines be left-to-right? Otherwise right to left.
*
* @param hor Should text lines be horizontal? Otherwise vertical.
*
* @return A ComponentOrientation value with the specified values for
* left-to-right-ness and verticality. If an exception occurs,
* a default value of LEFT_TO_RIGHT is returned.
*/
public static ComponentOrientation componentOrientationFactory(
boolean ltr, boolean hor) {
try {
Class<ComponentOrientation> coClass = ComponentOrientation.class;
Constructor<ComponentOrientation> conint
= coClass.getDeclaredConstructor(int.class);
conint.setAccessible(true);
int dir = (ltr ? LTR_BIT : 0) + (hor ? HORIZ_BIT : 0);
return conint.newInstance(dir);
} catch (NoSuchMethodException | InstantiationException
| IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NullPointerException ex) {
return ComponentOrientation.LEFT_TO_RIGHT;
}
}

关于java - 如何为 BoxLayout 创建垂直 javax.swing.ComponentOrientation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25711729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com