📄 aqualayoutstyle.java
字号:
{ "CheckBoxUI", "RadioButtonUI",
new Insets(16, 24, 16, 24), new Insets(20, 24, 20, 24),
new Insets(24, 24, 24, 24) },
{ "default",
new Insets(16, 16, 16, 16), new Insets(20, 20, 20, 20),
new Insets(24, 24, 24, 24) },
};
/**
* The visualMarginDefinition table defines the visually perceived
* margin of the components.
*
* This array is used to initialize the visualMargins HashMap.
*
* The array has the following structure, which is supposed to be a
* a compromise between legibility and code size.
* visualMarginDefinitions[0..n] = preferred gaps for a party of a two related UI's
* visualMarginDefinitions[][0..m-1] = name of UI
* optionally followed by a full stop and
* a style name
* containerGapDefinitions[][m] = visual margins
*/
private final static Object[][] visualMarginDefinitions = {
// UI, regular
{ "ButtonUI", "ButtonUI.text",
"ToggleButtonUI", "ToggleButtonUI.text",
new Insets(5, 3, 3, 3)
},
{ "ButtonUI.icon",
"ToggleButtonUI.icon",
new Insets(5, 2, 3, 2)
},
{ "ButtonUI.toolbar",
"ToggleButtonUI.toolbar",
new Insets(0, 0, 0, 0)
},
{ "CheckBoxUI", new Insets(4, 4, 3, 3) },
{ "ComboBoxUI", new Insets(2, 3, 4, 3) },
{ "DesktopPaneUI", new Insets(0, 0, 0, 0) },
{ "EditorPaneUI", "TextAreaUI", "TextPaneUI",
new Insets(0, 0, 0, 0)
},
{ "FormattedTextFieldUI", "PasswordFieldUI", "TextFieldUI",
new Insets(0, 0, 0, 0)
},
{ "LabelUI", new Insets(0, 0, 0, 0) },
{ "ListUI", new Insets(0, 0, 0, 0) },
{ "PanelUI", new Insets(0, 0, 0, 0) },
{ "ProgressBarUI", "ProgressBarUI.horizontal", new Insets(0, 2, 4, 2) },
{ "ProgressBarUI.vertical", new Insets(2, 0, 2, 4) },
{ "RadioButtonUI", new Insets(4, 4, 3, 3) },
{ "ScrollBarUI", new Insets(0, 0, 0, 0) },
{ "ScrollPaneUI", new Insets(0, 0, 0, 0) },
{ "SpinnerUI", new Insets(0, 0, 0, 0) },
{ "SeparatorUI", new Insets(0, 0, 0, 0) },
{ "SplitPaneUI", new Insets(0, 0, 0, 0) },
{ "SliderUI", "SliderUI.horizontal", new Insets(3, 6, 3, 6) },
{ "SliderUI.vertical", new Insets(6, 3, 6, 3) },
{ "TabbedPaneUI", "TabbedPaneUI.top", new Insets(5, 7, 10, 7) },
{ "TabbedPaneUI.bottom", new Insets(4, 7, 5, 7) },
{ "TabbedPaneUI.left", new Insets(4, 6, 10, 7) },
{ "TabbedPaneUI.right", new Insets(4, 7, 10, 6) },
{ "TableUI", new Insets(0, 0, 0, 0) },
{ "TreeUI", new Insets(0, 0, 0, 0) },
{ "default", new Insets(0, 0, 0, 0) },
};
/**
* The relatedGaps map defines the preferred gaps
* of one party of two related components.
*/
private final static Map RELATED_GAPS = createInsetsMap(relatedGapDefinitions);
/**
* The unrelatedGaps map defines the preferred gaps
* of one party of two unrelated components.
*/
private final static Map UNRELATED_GAPS = createInsetsMap(unrelatedGapDefinitions);
/**
* The containerGaps map defines the preferred insets (child gaps)
* of a parent component towards one of its children.
*/
private final static Map CONTAINER_GAPS = createInsetsMap(containerGapDefinitions);
/**
* The indentGaps map defines the preferred indentation
* for components that are indented after the specified component.
*/
private final static Map INDENT_GAPS = createInsetsMap(indentGapDefinitions);
/**
* The visualMargins map defines the preferred indentation
* for components that are indented after the specified component.
*/
private final static Map VISUAL_MARGINS = createInsetsMap(visualMarginDefinitions);
/**
* Creates a map for the specified definitions array.
* <p>
* The key for the map is the name of the UI, for example, ButtonUI, with
* a value of ComponentInsets. Each ComponentInsets may have sub styles.
*/
// private static Map<String,ComponentInsets> createInsetsMap(Object[][] definitions) {
private static Map createInsetsMap(Object[][] definitions) {
Map map = new HashMap();
for (int i=0; i < definitions.length; i++) {
int keys = 0;
while (keys < definitions[i].length &&
(definitions[i][keys] instanceof String)) {
keys++;
}
Insets[] values = new Insets[definitions[i].length - keys];
for (int j=keys; j < definitions[i].length; j++) {
values[j-keys] = (Insets) definitions[i][j];
}
for (int j=0; j < keys; j++) {
String key = (String)definitions[i][j];
int subindex = key.indexOf('.');
if (subindex == -1) {
ComponentInsets componentInsets = (ComponentInsets)map.get(key);
if (componentInsets == null) {
componentInsets = new ComponentInsets(values);
map.put(key, new ComponentInsets(values));
} else {
assert (componentInsets.getInsets() == null);
componentInsets.setInsets(values);
}
} else {
String subkey = key.substring(subindex + 1);
String parentKey = key.substring(0, subindex);
ComponentInsets componentInsets = (ComponentInsets)
map.get(parentKey);
if (componentInsets == null) {
componentInsets = new ComponentInsets();
map.put(parentKey, componentInsets);
}
componentInsets.addSubinsets(subkey,
new ComponentInsets(values));
}
}
}
return map;
}
public static void main(String[] args) {
JButton button = new JButton();
button.putClientProperty("JButton.buttonType", "metal");
JButton button2 = new JButton();
LayoutStyle style = new AquaLayoutStyle();
int gap =
style.getPreferredGap(button, button2, LayoutStyle.RELATED, SwingConstants.EAST,
null);
System.err.println("gap= " + gap);
button.putClientProperty("JButton.buttonType", "square");
button2.putClientProperty("JButton.buttonType", "square");
gap =
style.getPreferredGap(button, button2, LayoutStyle.RELATED, SwingConstants.EAST,
null);
System.err.println("gap= " + gap);
}
/**
* Creates a new instance.
*/
public AquaLayoutStyle() {
}
/**
* Returns the amount of space to use between two components.
* The return value indicates the distance to place
* <code>component2</code> relative to <code>component1</code>.
* For example, the following returns the amount of space to place
* between <code>component2</code> and <code>component1</code>
* when <code>component2</code> is placed vertically above
* <code>component1</code>:
* <pre>
* int gap = getPreferredGap(component1, component2,
* LayoutStyle.RELATED,
* SwingConstants.NORTH, parent);
* </pre>
* The <code>type</code> parameter indicates the type
* of gap being requested. It can be one of the following values:
* <table>
* <tr><td><code>RELATED</code>
* <td>If the two components will be contained in
* the same parent and are showing similar logically related
* items, use <code>RELATED</code>.
* <tr><td><code>UNRELATED</code>
* <td>If the two components will be
* contained in the same parent but show logically unrelated items
* use <code>UNRELATED</code>.
* <tr><td><code>INDENT</code>
* <td>Used to obtain the preferred distance to indent a component
* relative to another. For example, if you want to horizontally
* indent a JCheckBox relative to a JLabel use <code>INDENT</code>.
* This is only useful for the horizontal axis.
* </table>
* <p>
* It's important to note that some look and feels may not distinguish
* between <code>RELATED</code> and <code>UNRELATED</code>.
* <p>
* The return value is not intended to take into account the
* current size and position of <code>component2</code> or
* <code>component1</code>. The return value may take into
* consideration various properties of the components. For
* example, the space may vary based on font size, or the preferred
* size of the component.
*
* @param component1 the <code>JComponent</code>
* <code>component2</code> is being placed relative to
* @param component2 the <code>JComponent</code> being placed
* @param type how the two components are being placed
* @param position the position <code>component2</code> is being placed
* relative to <code>component1</code>; one of
* <code>SwingConstants.NORTH</code>,
* <code>SwingConstants.SOUTH</code>,
* <code>SwingConstants.EAST</code> or
* <code>SwingConstants.WEST</code>
* @param parent the parent of <code>component2</code>; this may differ
* from the actual parent and may be null
* @return the amount of space to place between the two components
* @throws IllegalArgumentException if <code>position</code> is not
* one of <code>SwingConstants.NORTH</code>,
* <code>SwingConstants.SOUTH</code>,
* <code>SwingConstants.EAST</code> or
* <code>SwingConstants.WEST</code>; <code>type</code> not one
* of <code>INDENT</code>, <code>RELATED</code>
* or <code>UNRELATED</code>; or <code>component1</code> or
* <code>component2</code> is null
*/
public int getPreferredGap(JComponent component1, JComponent component2,
int type, int position, Container parent) {
// Check args
super.getPreferredGap(component1, component2, type, position, parent);
int result;
// Compute gap
if (type == INDENT) {
// Compute gap
if (position == SwingConstants.EAST || position == SwingConstants.WEST) {
int gap = getButtonChildIndent(component1, position);
if (gap != 0) {
return gap;
}
}
int sizeStyle = getSizeStyle(component1);
Insets gap1 = getPreferredGap(component1, type, sizeStyle);
switch (position) {
case SwingConstants.NORTH :
result = gap1.bottom;
break;
case SwingConstants.SOUTH :
result = gap1.top;
break;
case SwingConstants.EAST :
result = gap1.left;
break;
case SwingConstants.WEST :
default :
result = gap1.right;
break;
}
int raw = result;
// Compensate for visual margin
Insets visualMargin2 = getVisualMargin(component2);
switch (position) {
case SwingConstants.NORTH :
result -= visualMargin2.bottom;
break;
case SwingConstants.SOUTH :
result -= visualMargin2.top;
break;
case SwingConstants.EAST :
result -= visualMargin2.left;
break;
case SwingConstants.WEST :
result -= visualMargin2.right;
default :
break;
}
} else {
// Compute gap
int sizeStyle = Math.min(getSizeStyle(component1),
getSizeStyle(component2));
Insets gap1 = getPreferredGap(component1, type, sizeStyle);
Insets gap2 = getPreferredGap(component2, type, sizeStyle);
switch (position) {
case SwingConstants.NORTH :
result = Math.max(gap1.top, gap2.bottom);
break;
case SwingConstants.SOUTH :
result = Math.max(gap1.bottom, gap2.top);
break;
case SwingConstants.EAST :
result = Math.max(gap1.right, gap2.left);
break;
case SwingConstants.WEST :
default :
result = Math.max(gap1.left, gap2.right);
break;
}
// Compensate for visual margin
Insets visualMargin1 = getVisualMargin(component1);
Insets visualMargin2 = getVisualMargin(component2);
switch (position) {
case SwingConstants.NORTH :
result -= visualMargin1.top + visualMargin2.bottom;
break;
case SwingConstants.SOUTH :
result -= visualMargin1.bottom + visualMargin2.top;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -