📄 swingutilities.java
字号:
rectCount++;
}
} else {
/* 1 */
if (rectB.x <= rectA.x && rectB.y <= rectA.y) {
if ((rectB.x + rectB.width) > (rectA.x + rectA.width)) {
t.x = rectA.x; t.y = rectB.y + rectB.height;
t.width = rectA.width; t.height = rectA.y + rectA.height - (rectB.y + rectB.height);
if(t.width > 0 && t.height > 0) {
a = t;
rectCount++;
}
} else if ((rectB.y + rectB.height) > (rectA.y + rectA.height)) {
t.setBounds((rectB.x + rectB.width), rectA.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
if(t.width > 0 && t.height > 0) {
a = t;
rectCount++;
}
} else {
t.setBounds((rectB.x + rectB.width), rectA.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width),
(rectB.y + rectB.height) - rectA.y);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
(rectA.y + rectA.height) - (rectB.y + rectB.height));
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
}
} else if (rectB.x <= rectA.x && (rectB.y + rectB.height) >= (rectA.y + rectA.height)) {
if ((rectB.x + rectB.width) > (rectA.x + rectA.width)) {
t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
if(t.width > 0 && t.height > 0) {
a = t;
rectCount++;
}
} else {
t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds((rectB.x + rectB.width), rectB.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width),
(rectA.y + rectA.height) - rectB.y);
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
}
} else if (rectB.x <= rectA.x) {
if ((rectB.x + rectB.width) >= (rectA.x + rectA.width)) {
t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
if(t.width>0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
(rectA.y + rectA.height) - (rectB.y + rectB.height));
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
} else {
t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds((rectB.x + rectB.width), rectB.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width),
rectB.height);
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
(rectA.y + rectA.height) - (rectB.y + rectB.height));
if(t.width > 0 && t.height > 0) {
c = new Rectangle(t);
rectCount++;
}
}
} else if (rectB.x <= (rectA.x + rectA.width) && (rectB.x + rectB.width) > (rectA.x + rectA.width)) {
if (rectB.y <= rectA.y && (rectB.y + rectB.height) > (rectA.y + rectA.height)) {
t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
if(t.width > 0 && t.height > 0) {
a = t;
rectCount++;
}
} else if (rectB.y <= rectA.y) {
t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x,
(rectB.y + rectB.height) - rectA.y);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
(rectA.y + rectA.height) - (rectB.y + rectB.height));
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
} else if ((rectB.y + rectB.height) > (rectA.y + rectA.height)) {
t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, rectB.y, rectB.x - rectA.x,
(rectA.y + rectA.height) - rectB.y);
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
} else {
t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, rectB.y, rectB.x - rectA.x,
rectB.height);
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
(rectA.y + rectA.height) - (rectB.y + rectB.height));
if(t.width > 0 && t.height > 0) {
c = new Rectangle(t);
rectCount++;
}
}
} else if (rectB.x >= rectA.x && (rectB.x + rectB.width) <= (rectA.x + rectA.width)) {
if (rectB.y <= rectA.y && (rectB.y + rectB.height) > (rectA.y + rectA.height)) {
t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds((rectB.x + rectB.width), rectA.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
} else if (rectB.y <= rectA.y) {
t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectB.x, (rectB.y + rectB.height),
rectB.width,
(rectA.y + rectA.height) - (rectB.y + rectB.height));
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
t.setBounds((rectB.x + rectB.width), rectA.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
if(t.width > 0 && t.height > 0) {
c = new Rectangle(t);
rectCount++;
}
} else {
t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectB.x, rectA.y, rectB.width,
rectB.y - rectA.y);
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
t.setBounds((rectB.x + rectB.width), rectA.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
if(t.width > 0 && t.height > 0) {
c = new Rectangle(t);
rectCount++;
}
}
}
}
result = new Rectangle[rectCount];
rectCount = 0;
if(a != null)
result[rectCount++] = a;
if(b != null)
result[rectCount++] = b;
if(c != null)
result[rectCount++] = c;
if(d != null)
result[rectCount++] = d;
return result;
}
/**
* Returns true if the mouse event specifies the left mouse button.
*
* @param anEvent a MouseEvent object
* @return true if the left mouse button was active
*/
public static boolean isLeftMouseButton(MouseEvent anEvent) {
if (is1dot2) {
return ((anEvent.getModifiers() & InputEvent.BUTTON1_MASK) != 0);
}
return ((anEvent.getModifiers() & InputEvent.BUTTON1_MASK) != 0 ||
// Workaround for Solaris not setting BUTTON1_MASK
// Not needed in 1.2 where BUTTON1_MASK is correctly set.
(anEvent.getModifiers() & (InputEvent.BUTTON2_MASK |
InputEvent.BUTTON3_MASK)) == 0);
}
/**
* Returns true if the mouse event specifies the middle mouse button.
*
* @param anEvent a MouseEvent object
* @return true if the middle mouse button was active
*/
public static boolean isMiddleMouseButton(MouseEvent anEvent) {
return ((anEvent.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK);
}
/**
* Returns true if the mouse event specifies the right mouse button.
*
* @param anEvent a MouseEvent object
* @return true if the right mouse button was active
*/
public static boolean isRightMouseButton(MouseEvent anEvent) {
return ((anEvent.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK);
}
/*
* Returns whether this is being run on a JDK 1.2 or later VM.
* This is a system-wide, rather than AppContext-wide, state.
*/
/*package-private*/ static boolean is1dot2 = true;
static {
try {
// Test if method introduced in 1.2 is available.
Method m = Class.class.getMethod("getPackage", null);
is1dot2 = (m != null);
} catch (NoSuchMethodException e) {
is1dot2 = false;
}
// Warn if running wrong version of this class for this JDK.
if (is1dot2) {
System.err.println("warning: running 1.1 version of SwingUtilities");
}
}
/**
* Compute the width of the string using a font with the specified
* "metrics" (sizes).
*
* @param fm a FontMetrics object to compute with
* @param str the String to compute
* @return an int containing the string width
*/
public static int computeStringWidth(FontMetrics fm,String str) {
if (is1dot2) {
// You can't assume that a string's width is the sum of its
// characters' widths in Java2D -- it may be smaller due to
// kerning, etc.
return fm.stringWidth(str);
}
int w[] = fm.getWidths();
int i,c;
int result = 0;
char ch;
for(i=0,c=str.length() ; i < c ; i++) {
ch = str.charAt(i);
if(ch > 255)
return fm.stringWidth(str);
else
result += w[(int)ch];
}
return result;
}
/**
* Compute and return the location of the icons origin, the
* location of origin of the text baseline, and a possibly clipped
* version of the compound labels string. Locations are computed
* relative to the viewR rectangle.
* The JComponents orientation (LEADING/TRAILING) will also be taken
* into account and translated into LEFT/RIGHT values accordingly.
*/
public static String layoutCompoundLabel(JComponent c,
FontMetrics fm,
String text,
Icon icon,
int verticalAlignment,
int horizontalAlignment,
int verticalTextPosition,
int horizontalTextPosition,
Rectangle viewR,
Rectangle iconR,
Rectangle textR,
int textIconGap)
{
boolean orientationIsLeftToRight = true;
int hAlign = horizontalAlignment;
int hTextPos = horizontalTextPosition;
// Translate LEADING/TRAILING values in horizontalAlignment
// to LEFT/RIGHT values depending on the components orientation
switch (horizontalAlignment) {
case LEADING:
hAlign = (orientationIsLeftToRight) ? LEFT : RIGHT;
break;
case TRAILING:
hAlign = (orientationIsLeftToRight) ? RIGHT : LEFT;
break;
}
// Translate LEADING/TRAILING values in horizontalTextPosition
// to LEFT/RIGHT values depending on the components orientation
switch (horizontalTextPosition) {
case LEADING:
hTextPos = (orientationIsLeftToRight) ? LEFT : RIGHT;
break;
case TRAILING:
hTextPos = (orientationIsLeftToRight) ? RIGHT : LEFT;
break;
}
return layoutCompoundLabelImpl(c,
fm,
text,
icon,
verticalAlignment,
hAlign,
verticalTextPosition,
hTextPos,
viewR,
iconR,
textR,
textIconGap);
}
/**
* Compute and return the location of the icons origin, the
* location of origin of the text baseline, and a possibly clipped
* version of the compound labels string. Locations are computed
* relative to the viewR rectangle.
* This layoutCompoundLabel() does not know how to handle LEADING/TRAILING
* values in horizontalTextPosition (they will default to RIGHT) and in
* horizontalAlignment (they will default to CENTER).
* Use the other version of layoutCompoundLabel() instead.
*/
public static String layoutCompoundLabel(
FontMetrics fm,
String text,
Icon icon,
int verticalAlignment,
int horizontalAlignment,
int verticalTextPosition,
int horizontalTextPosition,
Rectangle viewR,
Rectangle iconR,
Rectangle textR,
int textIconGap)
{
return layoutCompoundLabelImpl(null, fm, text, icon,
verticalAlignment,
horizontalAlignment,
verticalTextPosition,
horizontalTextPosition,
viewR, iconR, textR, textIconGap);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -