⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsframe.java

📁 Java自定义窗体JsFrame。简介见:http://jason0086.spaces.live.com/Blog/cns!A797D0C5C0C13C92!518.entry
💻 JAVA
字号:
package com.hfkj.jsframe.frame;

import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.JComponent;
import javax.swing.JFrame;

import com.hfkj.jsframe.border.JsFrameBorder;
import com.hfkj.jsframe.titlebar.JsTitleBar;
import com.hfkj.jsframe.titlebar.JsTitleBarLine;

/**
 * An extended version of <code>java.awt.Frame</code> that supports a demonstrate frame
 * with its own frame border and title bar.
 * It uses the <code>JsFrameLayout</code> as its layout manager, which contains thirteen
 * compoennts associated with the following constaints as their location in the layout:
 * <p>
 * <ul>
 * <li><code>NORTHWEST</code>(defined in <code>JsFrameLayout</code>):
 * The northwest portion of the layout for this frame.
 * <li><code>NORTH</code>(defined in <code>JsFrameLayout</code>):
 * The north portion of the layout for this frame.
 * <li><code>NORTHEAST</code>(defined in <code>JsFrameLayout</code>):
 * The northeast portion of the layout for this frame.
 * <li><code>WEST</code>(defined in <code>JsFrameLayout</code>):
 * The west portion of the layout for this frame.
 * <li><code>TITLE</code>(defined in <code>JsFrameLayout</code>):
 * The title  portion of the layout for this frame.
 * <li><code>TITLE_LINE</code>(defined in <code>JsFrameLayout</code>):
 * The title line portion of the layout for this frame.
 * <li><code>MENU</code>(defined in <code>JsFrameLayout</code>):
 * The menu portion of the layout for this frame.
 * <li><code>CONTENT</code>(defined in <code>JsFrameLayout</code>):
 * The content portion of the layout for this frame.
 * <li><code>STATE</code>(defined in <code>JsFrameLayout</code>):
 * The state portion of the layout for this frame.
 * <li><code>SOUTHWEST</code>(defined in <code>JsFrameLayout</code>):
 * The southwest portion of the layout for this frame.
 * <li><code>SOUTH</code>(defined in <code>JsFrameLayout</code>):
 * The south portion of the layout for this frame.
 * <li><code>SOUTHEAST</code>(defined in <code>JsFrameLayout</code>):
 * The southeast portion of the layout for this frame.
 * </ul>
 * <p>
 * If you just want to operate some of the above portions, you should first get it 
 * from the layout of the <code>JsFrame</code> by doing the following:
 * <pre>
 * JsFrameLayout layout = (JsFrameLayout) jsframe.getLayout();
 * Component northwest = layout.getLayoutComponent(JsFrameLayout.NORTHWEST);
 * </pre>
 * or doing the following when the component added to this frame is <code>JsFrameBorderBlock</code>:
 * <pre>
 * JsFrameLayout layout = (JsFrameLayout) jsframe.getLayout();
 * JsFrameBorderBlock northwest = (JsFrameBorderBlock) layout.getLayoutComponent(JsFrameLayout.NORTHWEST);
 * </pre>
 * <p>
 * In fact, a <code>JsFrame</code> object has a <code>JsFrameBorder</code> object
 * which contains the following portions of the frame:
 * <code>NORTHWEST</code>, <code>NORTH</code>, <code>NORTHEAST</code>, <code>WEST</code>, <code>EAST</code>, <code>SOUTHWEST</code>,
 * <code>SOUTH</code>, <code>SOUTHEAST</code>. And all the <code>JsFrameBorderBlock</code> in the <code>JsFrameBorder</code> object
 * may respond the mouse events to resize the frame.
 * Each of the above frame border portion can be get from the frame directly by doing the following:
 * <pre>
 * JsFrameLayout layout = (JsFrameLayout) jsframe.getLayout();
 * Component northwest = layout.getLayoutComponent(JsFrameLayout.NORTHWEST);
 * </pre>
 * <p>
 * Additionally, a <code>JsFrame</code> object has a <code>JsTitleBar</code> object
 * which takes up the <code>TITLE</code> portion of the frame. And the <code>JsTitleBar</code>
 * object uses the <code>JsTitleBarLayout</code> as its layout manager, which contains five components
 * associated with the following constaints as their locations in the layout:
 * <code>ICON</code>, <code>TITLE</code>, <code>MINIMIZE</code>, <code>MAXIMIZE_RESTORE</code>, <code>CLOSE</code>.
 * By the <code>JsTitleBar</code> object, the frame can be moved, minimized, maximized and closed(disposed)
 * as the mouse operates on the <code>JsTitleBar</code> object.
 * If you want to change some of the <code>JsTitleBar</code> of the frame, you should get the portion first
 * by doing the following:
 * <pre>
 * JsFrameLayout frameLayout = (JsFrameLayout) jsframe.getLayout();
 * JsTitleBar frameTitle = (JsTitleBar) frameLayout.getLayoutComponent(JsFrameLayout.TITLE);
 * JsTitleBarLayout titleLayout = (JsTitleBarLayout) frameTitle.getLayout();
 * Component icon = titleLayout.getLayoutComponent(JsTitleBarLayout.ICON);
 * Component title = titleLayout.getLayoutComponent(JsTitleBarLayout.TITLE);
 * Component minimize = titleLayout.getLayoutComponent(JsTitleBarLayout.MINIMIZE);
 * Component maximize = titleLayout.getLayoutComponent(JsTitleBarLayout.MAXIMIZE_RESTORE);
 * Component close = titleLayout.getLayoutComponent(JsTitleBarLayout.CLOSE);
 * </pre>
 * or doing the following:
 * <pre>
 * JsFrameLayout frameLayout = (JsFrameLayout) jsframe.getLayout();
 * JsTitleBar frameTitle = (JsTitleBar) frameLayout.getLayoutComponent(JsFrameLayout.TITLE);
 * JsTitleBarLayout titleLayout = (JsTitleBarLayout) frameTitle.getLayout();
 * JLabel icon = (JLabel) titleLayout.getLayoutComponent(JsTitleBarLayout.ICON);
 * JLabel title = (JLabel) titleLayout.getLayoutComponent(JsTitleBarLayout.TITLE);
 * JButton minimize = (JButton) titleLayout.getLayoutComponent(JsTitleBarLayout.MINIMIZE);
 * JButton maximize = (JButton) titleLayout.getLayoutComponent(JsTitleBarLayout.MAXIMIZE_RESTORE);
 * JButton close = (JButton) titleLayout.getLayoutComponent(JsTitleBarLayout.CLOSE);
 * </pre>
 * <p>
 * And a <code>JsFrame</code> has a <code>JsTitleBarLine</code> which draws a line
 * with the gradually changing colors. The color of the title line would change gradually from outside
 * color at the start to inside color at the midpoint, then to outside color at the three-quarter point,
 * and then to inside color at the end.
 * 
 * @version 1.0 01/05/09
 * @author Jason (MSN:www.jason0086.com@hotmail.com)
 */
public class JsFrame extends JFrame {
	
	/**
	 * The layout for this js frame.
	 */
	private JsFrameLayout layoutJfl = null;
	
	/**
	 * The js frame border for this js frame.
	 */
	private JsFrameBorder borderJfb = null;
	
	private static final long serialVersionUID = 2190889408218809796L;
	
	/**
     * Constructs a js frame.
	 */
    public JsFrame() {
        super();
        this.setupJsFrame();
    }

    /**
     * Creates a new js frame in the specify GraphicsConfiguration of a screen device.
     * @param gc the <code>GraphicsConfiguration</code> that is used
     * 		to construct the new <code>JsFrame</code>;
     * 		if <code>gc</code> is <code>null</code>, the system
     * 		default <code>GraphicsConfiguration</code> is assumed
     * @exception IllegalArgumentException if <code>gc</code> is not from
     * 		a screen device.  This exception is always thrown when
     *      GraphicsEnvironment.isHeadless() returns true.
     * @see java.awt.GraphicsEnvironment#isHeadless
     * @see JComponent#getDefaultLocale
     */
    public JsFrame(GraphicsConfiguration gc) {
        super(gc);
        this.setupJsFrame();
    }

    /**
     * Creates a new js frame with the specify title.
     * @param title the title to be displayed in the frame's border. 
     * 		A <code>null</code> value is treated as an empty string, ""
     */
    public JsFrame(String title) {
        super(title);
        this.setupJsFrame();
    	((JsTitleBar) this.layoutJfl.getLayoutComponent(JsFrameLayout.TITLE)).setTitle(title);
    }
    
    /**
     * Creates a new js frame with the specify title and the specify GraphicsConfiguration of a screen device.
     * @param title the title to be displayed in the frame's title bar. 
     * 		A <code>null</code> value is treated as an empty string, ""
     * @param gc the <code>GraphicsConfiguration</code> that is used
     * 		to construct the new <code>JsFrame</code>;
     * 		if <code>gc</code> is <code>null</code>, the system
     * 		default <code>GraphicsConfiguration</code> is assumed
     * @exception IllegalArgumentException if <code>gc</code> is not from
     * 		a screen device.  This exception is always thrown when
     *      GraphicsEnvironment.isHeadless() returns true.
     * @see java.awt.GraphicsEnvironment#isHeadless
     * @see JComponent#getDefaultLocale
     */
    public JsFrame(String title, GraphicsConfiguration gc) {
        super(title, gc);
        this.setupJsFrame();
    	((JsTitleBar) this.layoutJfl.getLayoutComponent(JsFrameLayout.TITLE)).setTitle(title);
    }
    
    /**
	 * Change the icon and string associated with the look and feel.
     *
     */
    public void changeLookAndFeel() {
    	((JsTitleBar) this.layoutJfl.getLayoutComponent(JsFrameLayout.TITLE)).changeLookAndFeel();
    	((JsTitleBarLine) this.layoutJfl.getLayoutComponent(JsFrameLayout.TITLE_LINE)).changeLookAndFeel();
    	this.borderJfb.changeLookAndFeel();
    }
    
    /**
     * Sets the js frame border for this js frame.
     * @param frameBorder the border for this frame
     */
    public void setBorder(JsFrameBorder frameBorder) {
    	this.borderJfb = frameBorder;
    }
    
    /**
     * Returns the js frame border of this js frame.
     * @return the border of this frame
     */
    public JsFrameBorder getBorder() {
    	return this.borderJfb;
    }
    
    @Override
    public LayoutManager getLayout() {
    	return this.layoutJfl;
    }
    
    @Override
    public void setResizable(boolean resizabled) {
    	super.setResizable(resizabled);
    	this.borderJfb.setResizable(resizabled);
    }
    
    @Override
    public void setIconImage(Image image) {
    	super.setIconImage(image);
    	((JsTitleBar) this.layoutJfl.getLayoutComponent(JsFrameLayout.TITLE)).setIconImage(image);
    }
    
    @Override
    public void setTitle(String title) {
    	super.setTitle(title);
    	((JsTitleBar) this.layoutJfl.getLayoutComponent(JsFrameLayout.TITLE)).setTitle(title);
    }
    
    protected void setupJsFrame() {
    	this.setUndecorated(true);
    	// set the layout for this frame with the given int as the vertical gap between components
    	this.layoutJfl = new JsFrameLayout(3);
    	this.setLayout(this.layoutJfl);
		// add the title portion to this frame
		this.add(new JsTitleBar(this), JsFrameLayout.TITLE);
		// add the title line portion to this frame
    	this.add(new JsTitleBarLine(), JsFrameLayout.TITLE_LINE);
    	// set the border and the title line for this frame
    	this.setBorder(new JsFrameBorder(this));
    	// set the miximized bouds for this frame
    	this.setMaximizedBounds4JsFrame();
	}
    
    private void setMaximizedBounds4JsFrame() {
		int x = 0;
		int y = 0;
		int width = 0;
		int height = 0;
		Insets scrrenIns = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());
		Dimension  screenDmn = Toolkit.getDefaultToolkit().getScreenSize();
		x = -this.layoutJfl.getLayoutComponent(JsFrameLayout.WEST).getPreferredSize().width;
		y = -this.layoutJfl.getLayoutComponent(JsFrameLayout.NORTH).getPreferredSize().height;
		width = screenDmn.width 
				+ this.layoutJfl.getLayoutComponent(JsFrameLayout.WEST).getPreferredSize().width 
				+ this.layoutJfl.getLayoutComponent(JsFrameLayout.EAST).getPreferredSize().width 
				- scrrenIns.left 
				- scrrenIns.right;
		height = screenDmn.height 
					+ this.layoutJfl.getLayoutComponent(JsFrameLayout.NORTH).getPreferredSize().height 
					+ this.layoutJfl.getLayoutComponent(JsFrameLayout.SOUTH).getPreferredSize().height 
					- scrrenIns.top 
					- scrrenIns.bottom;
		this.setMaximizedBounds(new Rectangle(x, y, width, height));
    	
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -