📄 graphicutils.java
字号:
if (y + size.height > screenSize.height)
y = screenSize.height - size.height;
if (y < 0)
y = 0;
window.setLocation(x, y);
}
/**
* @return returns true if the component of one of its child has the focus
*/
public static boolean isAncestorOfFocusedComponent(Component c) {
if (c.hasFocus()) {
return true;
} else {
if (c instanceof Container) {
Container cont = (Container) c;
int n = cont.getComponentCount();
for (int i = 0; i < n; i++) {
Component child = cont.getComponent(i);
if (isAncestorOfFocusedComponent(child))
return true;
}
}
}
return false;
}
/**
* Returns the first component in the tree of <code>c</code> that can accept
* the focus.
*
* @param c the root of the component hierarchy to search
* @see #focusComponentOrChild
* @deprecated replaced by {@link #getFocusableComponentOrChild(Component, boolean)}
*/
public static Component getFocusableComponentOrChild(Component c) {
return getFocusableComponentOrChild(c, false);
}
/**
* Returns the first component in the tree of <code>c</code> that can accept
* the focus.
*
* @param c the root of the component hierarchy to search
* @param deepest if <code>deepest</code> is true the method will return the first and deepest component that can accept the
* focus. For example, if both a child and its parent are focusable and <code>deepest</code> is true, the child is
* returned.
* @see #focusComponentOrChild
*/
public static Component getFocusableComponentOrChild(Component c, boolean deepest) {
if (c != null && c.isEnabled() && c.isVisible()) {
if (c instanceof Container) {
Container cont = (Container) c;
if (deepest == false) { // first one is a good one
if (c instanceof JComponent) {
JComponent jc = (JComponent) c;
if (jc.isRequestFocusEnabled()) {
return jc;
}
}
}
int n = cont.getComponentCount();
for (int i = 0; i < n; i++) {
Component child = cont.getComponent(i);
Component focused = getFocusableComponentOrChild(child, deepest);
if (focused != null) {
return focused;
}
}
if (c instanceof JComponent) {
if (deepest == true) {
JComponent jc = (JComponent) c;
if (jc.isRequestFocusEnabled()) {
return jc;
}
}
} else {
return c;
}
}
}
return null;
}
/**
* Puts the focus on the first component in the tree of <code>c</code> that
* can accept the focus.
*
* @see #getFocusableComponentOrChild
*/
public static Component focusComponentOrChild(Component c) {
return focusComponentOrChild(c, false);
}
/**
* Puts the focus on the first component in the tree of <code>c</code> that
* can accept the focus.
*
* @param c the root of the component hierarchy to search
* @param deepest if <code>deepest</code> is true the method will focus the first and deepest component that can
* accept the focus.
* For example, if both a child and its parent are focusable and <code>deepest</code> is true, the child is focused.
* @see #getFocusableComponentOrChild
*/
public static Component focusComponentOrChild(Component c, boolean deepest) {
final Component focusable = getFocusableComponentOrChild(c, deepest);
if (focusable != null) {
focusable.requestFocus();
}
return focusable;
}
/**
* Loads an {@link Image} named <code>imageName</code> as a resource
* relative to the Class <code>cls</code>. If the <code>Image</code> can
* not be loaded, then <code>null</code> is returned. Images loaded here
* will be added to an internal cache based upon the full {@link URL} to
* their location.
* <p/>
* <em>This method replaces legacy code from JDeveloper 3.x and earlier.</em>
*
* @see Class#getResource(String)
* @see Toolkit#createImage(URL)
*/
public static Image loadFromResource(String imageName, Class cls) {
try {
final URL url = cls.getResource(imageName);
if (url == null) {
return null;
}
Image image = (Image) imageCache.get(url.toString());
if (image == null) {
image = Toolkit.getDefaultToolkit().createImage(url);
imageCache.put(url.toString(), image);
}
return image;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Rectangle[] getScreenBounds() {
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice[] screenDevices = graphicsEnvironment.getScreenDevices();
Rectangle[] screenBounds = new Rectangle[screenDevices.length];
for (int i = 0; i < screenDevices.length; i++) {
GraphicsDevice screenDevice = screenDevices[i];
final GraphicsConfiguration defaultConfiguration = screenDevice.getDefaultConfiguration();
screenBounds[i] = defaultConfiguration.getBounds();
}
return screenBounds;
}
public static final void makeSameSize(JComponent[] comps) {
if (comps.length == 0) {
return;
}
int max = 0;
for (int i = 0; i < comps.length; i++) {
int w = comps[i].getPreferredSize().width;
max = w > max ? w : max;
}
Dimension dim = new Dimension(max, comps[0].getPreferredSize().height);
for (int i = 0; i < comps.length; i++) {
comps[i].setPreferredSize(dim);
}
}
/**
* Return the hexidecimal color from a java.awt.Color
*
* @param c
* @return hexadecimal string
*/
public static final String toHTMLColor(Color c) {
int color = c.getRGB();
color |= 0xff000000;
String s = Integer.toHexString(color);
return s.substring(2);
}
public static final String createToolTip(String text, int width) {
final String htmlColor = toHTMLColor(TOOLTIP_COLOR);
final String toolTip = "<html><table width=" + width + " bgColor=" + htmlColor + "><tr><td><b>" + text + "</b></td></tr></table></table>";
return toolTip;
}
public static final String createToolTip(String text) {
final String htmlColor = toHTMLColor(TOOLTIP_COLOR);
final String toolTip = "<html><table bgColor=" + htmlColor + "><tr><td><b>" + text + "</b></td></tr></table></table>";
return toolTip;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -