📄 iframe.java
字号:
/**
* Closes the frame and calls <code>System.exit(0)</code>.
* @param e the WindowChangeEvent from one of its children
*/
public void windowClosed(WindowChangeEvent e)
{
System.exit(0);
}
/**
* Computes the restore size of the frame based on the Windows standard of 2 restore
* states - restore maximized and restore minimized.
* @param e the WindowChangeEvent from one of its children.
*/
public void windowMaximized(WindowChangeEvent e)
{
// if the app is already maximized
if (getWidth() == IWTUtilities.getScreenWidth() && getHeight() == IWTUtilities.getScreenHeight())
{
// the app just opened and they don't have a restore size yet
if (RESTORE_WIDTH == IWTUtilities.getScreenWidth() && RESTORE_HEIGHT == IWTUtilities.getScreenHeight())
setSize(IWTUtilities.getRestoreSize());
// they've resized already
else
setSize(RESTORE_WIDTH, RESTORE_HEIGHT);
RESTORE_WIDTH = getWidth();
RESTORE_HEIGHT = getHeight();
super.setLocation(RESTORE_X, RESTORE_Y);
}
// it's not maximized
else
{
setSize(IWTUtilities.getScreenWidth(), IWTUtilities.getScreenHeight());
super.setLocation(0, 0);
}
repaint();
}
/**
* Minimizes the frame by minimizing it in the native OS's window management.
* @param e the WindowChangeEvent from one of its children
*/
public void windowMinimized(WindowChangeEvent e)
{
setExtendedState(Frame.ICONIFIED);
}
/**
* Moves the frame.
* @param e the WindowChangeEvent from one of its children
*/
public void windowMoved(WindowChangeEvent e)
{
super.setLocation(getLocation().x + e.getChangeX(), getLocation().y + e.getChangeY());
// BUG FIX - Restore coordinates were getting reset to 0 on window maximizes
RESTORE_X = (getLocation().x == 0) ? RESTORE_X : getLocation().x;
RESTORE_Y = (getLocation().y == 0) ? RESTORE_Y : getLocation().y;
repaint();
}
/**
* Resizes the frame.
* @param e the WindowChangeEvent from one of its children
*/
public void windowResized(WindowChangeEvent e)
{
if (!r.isResizing())
{
Thread t = new Thread(r);
t.start();
r.update(getLocation().x, getLocation().y, getWidth(), getHeight());
}
// they're still resizing
if (e.isDragging())
{
switch (e.getDirection())
{
case WindowChangeEvent.RESIZE_EAST :
{
r.update(getLocation().x, getLocation().y, e.getPosX(), getHeight());
break;
}
case WindowChangeEvent.RESIZE_NORTH :
{
r.update(
(int) getLocation().getX(),
(int) getLocation().getY() + e.getChangeY(),
getWidth(),
getHeight() - e.getChangeY());
break;
}
case WindowChangeEvent.RESIZE_NORTH_EAST :
{
r.update(
(int) getLocation().getX(),
(int) getLocation().getY() + e.getChangeY(),
e.getPosX(),
getHeight() - e.getChangeY());
break;
}
case WindowChangeEvent.RESIZE_NORTH_WEST :
{
r.update(
(int) getLocation().getX() + e.getChangeX(),
(int) getLocation().getY() + e.getChangeY(),
getWidth() - e.getChangeX(),
getHeight() - e.getChangeY());
break;
}
case WindowChangeEvent.RESIZE_SOUTH :
{
r.update(getLocation().x, getLocation().y, getWidth(), e.getPosY());
break;
}
case WindowChangeEvent.RESIZE_SOUTH_EAST :
{
r.update(getLocation().x, getLocation().y, e.getPosX(), e.getPosY());
break;
}
case WindowChangeEvent.RESIZE_SOUTH_WEST :
{
r.update(
(int) getLocation().getX() + e.getChangeX(),
(int) getLocation().getY(),
getWidth() - e.getChangeX(),
e.getPosY());
break;
}
case WindowChangeEvent.RESIZE_WEST :
{
r.update(
(int) getLocation().getX() + e.getChangeX(),
(int) getLocation().getY(),
getWidth() - e.getChangeX(),
getHeight());
break;
}
}
}
// they're done dragging, paint the window
else
{
r.stopResizing();
switch (e.getDirection())
{
case WindowChangeEvent.RESIZE_EAST :
{
setSize(e.getPosX(), getHeight());
break;
}
case WindowChangeEvent.RESIZE_NORTH :
{
setSize(getWidth(), getHeight() - e.getChangeY());
super.setLocation((int) getLocation().getX(), (int) getLocation().getY() + e.getChangeY());
break;
}
case WindowChangeEvent.RESIZE_NORTH_EAST :
{
setSize(e.getPosX(), getHeight() - e.getChangeY());
super.setLocation((int) getLocation().getX(), (int) getLocation().getY() + e.getChangeY());
break;
}
case WindowChangeEvent.RESIZE_NORTH_WEST :
{
setSize(getWidth() - e.getChangeX(), getHeight() - e.getChangeY());
super.setLocation((int) getLocation().getX() + e.getChangeX(),(int) getLocation().getY() + e.getChangeY());
break;
}
case WindowChangeEvent.RESIZE_SOUTH :
{
setSize(getWidth(), e.getPosY());
break;
}
case WindowChangeEvent.RESIZE_SOUTH_EAST :
{
setSize(e.getPosX(), e.getPosY());
break;
}
case WindowChangeEvent.RESIZE_SOUTH_WEST :
{
setSize(getWidth() - e.getChangeX(), e.getPosY());
super.setLocation((int) getLocation().getX() + e.getChangeX(), (int) getLocation().getY());
break;
}
case WindowChangeEvent.RESIZE_WEST :
{
setSize(getWidth() - e.getChangeX(), getHeight());
super.setLocation((int) getLocation().getX() + e.getChangeX(), (int) getLocation().getY());
break;
}
}
RESTORE_WIDTH = getWidth();
RESTORE_HEIGHT = getHeight();
RESTORE_X = getLocation().x;
RESTORE_Y = getLocation().y;
getTitleBar().setRestoreButtonState(true);
repaint();
}
}
/**
* Does nothing.
* @param e the WindowEvent
*/
public void windowOpened(WindowEvent e)
{
}
/**
* Does nothing.
* @param e the WindowEvent
*/
public void windowClosing(WindowEvent e)
{
}
/**
* Does nothing.
* @param e the WindowEvent
*/
public void windowClosed(WindowEvent e)
{
}
/**
* Does nothing.
* @param e the WindowEvent
*/
public void windowIconified(WindowEvent e)
{
}
/**
* Recaptures the screen shot if transparency is turned on.
* @param e the WindowEvent
*/
public void windowDeiconified(WindowEvent e)
{
setVisible(false);
setVisible(true);
}
boolean isActive = false;
/**
* Does nothing.
* @param e the WindowEvent
*/
public void windowActivated(WindowEvent e)
{
if (!isActive)
{
setVisible(false);
setVisible(true);
isActive = true;
}
}
/**
* Does nothing.
* @param e the WindowEvent
*/
public void windowDeactivated(WindowEvent e)
{
}
/**
* Captures mouse clicks from the root pane and relays them to the
* component at the event's coordinates.
* @param e the MouseEvent from the root pane
*/
public void mouseClicked(MouseEvent e)
{
getBorderComponent(e.getX(), e.getY()).mouseClicked(e);
}
/**
* Captures mouse presses from the root pane and relays them to the
* component at the event's coordinates.
* @param e the MouseEvent from the root pane
*/
public void mousePressed(MouseEvent e)
{
pressedComponent = getBorderComponent(e.getX(), e.getY());
if (pressedComponent.isTransparent(e.getX(), e.getY()))
{
toBack();
}
else
pressedComponent.mousePressed(e);
}
/**
* Captures mouse releases from the root pane and relays them to the
* component at the event's coordinates.
* @param e the MouseEvent from the root pane
*/
public void mouseReleased(MouseEvent e)
{
pressedComponent.mouseReleased(e);
}
/**
* Captures mouse entries from the root pane and relays them to the
* component at the event's coordinates.
* @param e the MouseEvent from the root pane
*/
public void mouseEntered(MouseEvent e)
{
getBorderComponent(e.getX(), e.getY()).mouseEntered(e);
}
/**
* Captures mouse exits from the root pane and relays them to the
* component at the event's coordinates.
* @param e the MouseEvent from the root pane
*/
public void mouseExited(MouseEvent e)
{
getBorderComponent(e.getX(), e.getY()).mouseExited(e);
}
/**
* Captures mouse drags from the root pane and relays them to the
* component at the event's coordinates.
* @param e the MouseEvent from the root pane
*/
public void mouseDragged(MouseEvent e)
{
pressedComponent.mouseDragged(e);
}
/**
* Captures mouse movements from the root pane and relays them to the
* component at the event's coordinates.
* @param e the MouseEvent from the root pane
*/
public void mouseMoved(MouseEvent e)
{
getBorderComponent(e.getX(), e.getY()).mouseMoved(e);
}
/**
* Returns the IBorderComponent at the given coordinates in the frame.
* @param x the x coordinate
* @param y the y coordinate
* @return the iborder component at the given mouse coordinates
*/
protected IBorderComponent getBorderComponent(int x, int y)
{
if (y < getTitleBarHeight())
return getTitleBar();
else
return getIContentPane();
}
/**
* The ResizeThread takes charge of resizing and revalidating the frame. Without
* the resize thread, resizing of the frame results in flickering and unsmooth
* resizing. By using the resize thread, flickering is removed and the resizing
* is much smoother.
* @author MAbernethy
*/
protected class ResizeThread implements Runnable
{
private int x;
private int y;
private int width;
private int height;
private boolean isResizing;
private JFrame w;
/**
* Creates a ResizeThread
* @param w the frame instance getting resized
* @param x the current x coordinate
* @param y the current y coordinate
* @param width the width of the frame
* @param height the height of the frame
*/
public ResizeThread(JFrame w, int x, int y, int width, int height)
{
this.w = w;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
/**
* Updates the size and location of the frame.
* @param x the new x coordinate
* @param y the new y coordinate
* @param width the new width
* @param height the new height
*/
public void update(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
/**
* Tells the thread to stop resizing the frame.
*/
public void stopResizing()
{
isResizing = false;
}
/**
* Returns whether the thread is currently resizing the frame.
* @return whether the thread is resizing
*/
public boolean isResizing()
{
return isResizing;
}
/**
* Resizes the frame and revalidates the content pane.
*/
public void run()
{
isResizing = true;
while (isResizing)
{
w.setBounds(x, y, width, height);
w.getContentPane().invalidate();
w.getContentPane().validate();
try
{
Thread.sleep(300);
}
catch (InterruptedException e){}
}
}
}
/**
* The default border that appears around the frame in Windows 2000.
* @author MAbernethy
*/
protected class DefaultBorder extends AbstractBorder
{
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h)
{
g.setColor(c.getBackground().brighter().brighter());
g.drawLine(0, 0, 0, h-1);
g.setColor(c.getBackground().brighter());
g.drawLine(1, 1, 1, h-2);
g.setColor(c.getBackground().darker().darker());
g.drawLine(1, h-1, w-1, h-1);
g.drawLine(w-1, 1, w-1, h-2);
g.setColor(c.getBackground().darker());
g.drawLine(2, h-2, w-2, h-2);
g.drawLine(w-2, 2, w-2, h-3);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -