📄 iframe.java
字号:
package com.ibm.iwt;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
import com.ibm.iwt.event.WindowChangeEvent;
import com.ibm.iwt.event.WindowChangeListener;
import com.ibm.iwt.util.IWTUtilities;
import com.ibm.iwt.window.IBorderComponent;
import com.ibm.iwt.window.IContentPane;
import com.ibm.iwt.window.IWindowTitleBar;
/**
* <p>The IFrame is the main class that is used to create
* a custom frame. In addition to the methods
* inherited from the IFrame's superclass, JFrame,
* there are public methods added to the IFrame that
* allow you to change some basic look and feel components on your frame.
* <p>The default IFrame behaves exactly the same as a JFrame does so that a
* JFrame and default IFrame can be interchangeable. However, by calling some of the
* public methods available in the IFrame, you can quickly change the look of
* your window with only a few lines of code.
* <p><b>Note:</b> The IFrame functions <code>getIContentPane()</code>
* and <code>setIContentPane()</code> should be used in place
* of the JFrame equivelent <code>getContentPane()</code> and
* <code>setContentPane()</code>. All components that get
* added to the IFrame should call <code>myIFrame.getIContentPane().add()</code>.
* Adding components to the IFrame using <code>getContentPane()</code>
* or setting the content pane using <code>setContentPane()</code>
* will lead to unpredictable and most likely incorrect behavior.
* @author MAbernethy
* <p><font size=-1><b>Known Issues</b>
* <br>+ When user clicks on transparent region of IFrame, IFrame is sent to back of all
* open windows, instead of just the window clicked on.
* <br>+ There is no minimum size for the windows, which can lead to layout issues on
* default title bars and disappearing windows which have 1 pixel width or height.
* <br>+ When the window loses focus, and then regains focus, it does not recapture
* the screen shot. For example, but minimizing a window behind the IFrame,
* correct behavior would be to recapture the screen shot without that minimized
* window. The solution causes slow performance when the IFrame regains
* focus and has been omitted. As a workaround, code can be added to
* windowDeactived() to forcefully minimize the window when it loses focus.
* </font>
*/
public class IFrame extends JFrame implements WindowChangeListener, WindowListener, MouseListener, MouseMotionListener
{
/** the instance of the title panel inside the frame */
protected IWindowTitleBar titleBar;
/** the instance of the icontent pane inside the frame */
protected IContentPane iContentPane;
private static int RESTORE_WIDTH = 0;
private static int RESTORE_HEIGHT = 0;
private static int RESTORE_X = 0;
private static int RESTORE_Y = 0;
/** constant to turn on transparency when frame is initialized */
protected int TRANSPARENT_STARTUP = -1;
/** constant to turn off transparency */
protected int TRANSPARENT_NONE = 0;
/** contant to turn on transparency */
protected int TRANSPARENT_ON = 1;
private int direction = WindowChangeEvent.RESIZE_NONE;
private int X = 0;
private int Y = 0;
private ResizeThread r;
private boolean isDrawingTransparent = false;
private IBorderComponent pressedComponent = null;
/** the screenshot that is used to draw transparency */
protected BufferedImage screenShot = null;
/** the robot instance that captures screen shots */
protected Robot robot;
/** the rectangle that defines the screen dimensions */
protected Rectangle rect;
/** the frame's transparency status - when off it will speed performance */
protected int transparentState = TRANSPARENT_STARTUP;
/**
* Creates an IFrame.
*/
public IFrame()
{
super();
setUndecorated(true);
initialize();
}
/**
* A convenience static method that returns the current application size in pixels.
* Subclasses of the IBorderComponent may find this method useful.
* @return the size of the frame in pixels
*/
public static Dimension getApplicationSize()
{
return new Dimension(RESTORE_WIDTH, RESTORE_HEIGHT);
}
/**
* Sets the frame visibility and also, if transparency is turned on, captures
* the screen shot.
* @param isVisible the visibility of the frame
*/
public void setVisible(boolean isVisible)
{
if (isVisible && transparentState != TRANSPARENT_NONE)
{
if (transparentState == TRANSPARENT_STARTUP)
transparentState = TRANSPARENT_NONE;
captureScreenShot();
}
super.setVisible(isVisible);
}
/**
* Sets the size of the frame.
* @param width the width in pixels
* @param height the height in pixels
*/
public void setSize(int width, int height)
{
super.setSize(width, height);
repaint();
}
/**
* Sets the size of the frame.
* @param size the size of the frame in pixels
*/
public void setSize(Dimension size)
{
setSize((int)size.getWidth(), (int)size.getHeight());
}
/**
* Sets the bounds of the frame.
* @param r the rectangle that defines the bounds
*/
public void setBounds(Rectangle r)
{
setBounds(r.x, r.y, r.width, r.height);
}
/**
* Sets the location of the frame.
* @param x the x position
* @param y the y position
*/
public void setLocation(int x, int y)
{
super.setLocation(x, y);
RESTORE_X = x;
RESTORE_Y = y;
}
/**
* Sets the title of the frame and passes the title on to the current title bar.
* @param title the title of the frame
*/
public void setTitle(String title)
{
super.setTitle(title);
getTitleBar().setTitle(title);
}
/**
* Sets the icon image of the frame and passes the icon on to the current title bar.
* @param i the icon of the frame
*/
public void setIconImage(Image i)
{
super.setIconImage(i);
getTitleBar().setLogo(new ImageIcon(i));
}
private void initialize()
{
addWindowListener(this);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(getTitleBar(), BorderLayout.NORTH);
getContentPane().add(getIContentPane(), BorderLayout.CENTER);
this.setSize(IWTUtilities.getScreenSize(this));
RESTORE_WIDTH = getWidth();
RESTORE_HEIGHT = getHeight();
r = new ResizeThread(this, 0, 0, getWidth(), getHeight());
getRootPane().addMouseListener(this);
getRootPane().addMouseMotionListener(this);
}
/**
* Returns the instance of the icontent pane that is being used by the frame.
* <p><b>Important Note:</b> This method should be used to add components to the
* interior of the frame, not <code>getContentPane()</code>. Using <code>
* getContentPane()</code> will cause unpredictable and most likely incorrect
* behavior. By default, the icontent pane instance has a BorderLayout layout and
* a default Windows 2000 border.
* @return the current icontent pane
*/
public IContentPane getIContentPane()
{
if (iContentPane == null)
{
iContentPane = new IContentPane();
iContentPane.setBorder(new DefaultBorder());
iContentPane.setLayout(new BorderLayout());
iContentPane.addWindowChangeListener(this);
}
return iContentPane;
}
/**
* Sets the current icontent pane of the frame.
* @param contentPane the new icontent pane
*/
public void setIContentPane(IContentPane contentPane)
{
getContentPane().add(contentPane, BorderLayout.CENTER);
iContentPane = contentPane;
iContentPane.addWindowChangeListener(this);
}
/**
* Returns the current title bar being used by the frame.
* @return the current title bar
*/
public IWindowTitleBar getTitleBar()
{
if (titleBar == null)
{
titleBar = new IWindowTitleBar();
titleBar.addWindowChangeListener(this);
}
return titleBar;
}
/**
* Sets the title bar on the frame.
* @param windowTitleBar the new title bar
*/
public void setTitleBar(IWindowTitleBar windowTitleBar)
{
getContentPane().remove(titleBar);
titleBar = windowTitleBar;
titleBar.addWindowChangeListener(this);
getContentPane().add(windowTitleBar, BorderLayout.NORTH);
repaint();
}
private void captureScreenShot()
{
try
{
robot = new Robot();
rect = new Rectangle(0, 0, IWTUtilities.getScreenWidth(), IWTUtilities.getScreenHeight());
screenShot = robot.createScreenCapture(rect);
}
catch (java.awt.AWTException ex) {}
}
/**
* Sets a region on the specified component transparent to the pixels behind the
* frame. To improve performance, the region that is being drawn transparent
* should be small, as painting the transparent regions is a relatively slow
* process compared to other repaints. Also, calls to this function should
* be called from the caller's <code>paint()</code> so that the transparent
* region repaints itself properly as well.
* @param c the component that will have the transparent region
* @param g the Graphics instance
* @param x the x coordinate of the rectangular transparent region
* @param y the y coordinate of the rectangular transparent region
* @param w the width of the rectangular transparent region
* @param h the height of the rectangular transparent region
*/
public void setTransparent(JComponent c, Graphics g, int x, int y, int w, int h)
{
transparentState = TRANSPARENT_ON;
Point p = new Point(x,y);
SwingUtilities.convertPointToScreen(p,c);
if (p.x+w > screenShot.getWidth())
w = screenShot.getWidth() - p.x;
if (p.y+h > screenShot.getHeight())
h = screenShot.getHeight() - p.y;
BufferedImage i = screenShot.getSubimage(Math.max(0,p.x), Math.max(0,p.y), w, h);
ImageIcon icon = new ImageIcon(i);
Point p2 = new Point(Math.max(0, p.x), Math.max(0, p.y));
SwingUtilities.convertPointFromScreen(p2, c);
icon.paintIcon(c, g, p2.x, p2.y);
}
/**
* Sets the solid background color of the title bar.
* @param background the background color
*/
public void setTitleBarBackground(Color background)
{
getTitleBar().setBackground(background);
}
/**
* Gets the background color of the title bar.
* @return the background color
*/
public Color getTitleBarBackground()
{
return getTitleBar().getBackground();
}
/**
* Sets the height of the title bar.
* @param height the height in pixels
*/
public void setTitleBarHeight(int height)
{
getTitleBar().setPreferredSize(new Dimension(1500, height));
}
/**
* Gets the height of the title bar.
* @return the height in pixels
*/
public int getTitleBarHeight()
{
return (int)getTitleBar().getPreferredSize().getHeight();
}
/**
* Sets the foreground and the background colors of the title bar buttons.
* @param foreground the foreground color
* @param background the background color
*/
public void setTitleBarButtonColors(Color foreground, Color background)
{
getTitleBar().setWindowButtonColors(background, foreground);
}
/**
* Gets the background color of the title bar buttons.
* @return the background color - note that since every title bar button can
* have a different color, this will be the background color of the first title bar
* button
*/
public Color getTitleBarButtonBackground()
{
return getTitleBar().getWindowButtonBackground();
}
/**
* Gets the foreground color of the title bar buttons.
* @return the foreground color - note that since every title bar button can
* have a different color, this will be the foreground color of the first title bar
* button
*/
public Color getTitleBarButtonForeground()
{
return getTitleBar().getWindowButtonForeground();
}
/**
* Sets the size of the title bar buttons.
* @param size the size in pixels
*/
public void setTitleBarButtonSize(Dimension size)
{
getTitleBar().setWindowButtonSize(size);
}
/**
* Gets the size of the title bar buttons.
* @return the size - note that since every title bar button can
* have a different size, this will be the size of the first title bar
* button
*/
public Dimension getTitleBarButtonSize()
{
return getTitleBar().getWindowButtonSize();
}
/**
* Sets the border of the current icontent pane.
* @param b the new border
*/
public void setIContentPaneBorder(Border b)
{
getIContentPane().setBorder(b);
}
/**
* Gets the border of the current icontent pane.
* @return the border of the icontent pane
*/
public Border getIContentPaneBorder()
{
return getIContentPane().getBorder();
}
/**
* Sets the border of the current title bar.
* @param b the new border
*/
public void setTitleBarBorder(Border b)
{
getTitleBar().setBorder(b);
}
/**
* Gets the border of the current title bar.
* @return the border of the title bar
*/
public Border getTitleBarBorder()
{
return getTitleBar().getBorder();
}
/**
* Sets the bounds of the frame.
* @param x the x coordinate for the upper left corner of the frame
* @param y the y coordinate for the upper left corner of the frame
* @param width the width of the frame
* @param height the height of the frame
*/
public void setBounds(int x, int y, int width, int height)
{
super.setBounds(x, y, width, height);
if (getRootPane() != null)
getContentPane().setBounds(0, 0, width, height);
repaint();
}
/**
* Repaints the frame and all of its children.
*/
public void repaint()
{
super.repaint();
if (getRootPane() != null)
{
getContentPane().invalidate();
getContentPane().validate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -