?? jstitlebar.java
字號:
package com.hfkj.jsframe.titlebar;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.UIManager;
import com.hfkj.jsframe.component.JsComponent;
/**
* The <code>JsTitleBar</code> uses <code>JsTitleBarLayout</code> layout manager,
* which contains five components associated with the following constaints
* as their location in the layout:
* <p>
* <ul>
* <li><code>ICON</code>(defined in <code>JsTitleBarLayout</code>):
* The icon portion in the layout of this title bar.
* <li><code>TITLE</code>(defined in <code>JsTitleBarLayout</code>):
* The title text portoin in the layout of this title bar.
* <li><code>MINIMIZE</code>(defined in <code>JsTitleBarLayout</code>):
* The minimize button portion in the layout of this title bar.
* <li><code>MAXIMIZE_RESTORE</code>(defined in <code>JsTitleBarLayout</code>):
* The maximize-restore button portion in the layout of this title bar.
* <li><code>CLOSE</code>(defined in <code>JsTitleBarLayout</code>):
* The close button portion in the layout of this title bar.
* <p>
* If you just want to operate some of the above portions, you should first get it
* from the layout of the <code>JsTitleBar</code> object by doing the following:
* <pre>
* Component icon = titleBar.getLayoutComponent(JsTitleBarLayout.ICON);
* Component title = titleBar.getLayoutComponent(JsTitleBarLayout.TITLE);
* Component minimize = titleBar.getLayoutComponent(JsTitleBarLayout.MINIMIZE);
* Component maximize = titleBar.getLayoutComponent(JsTitleBarLayout.MAXIMIZE_RESTORE);
* Component close = titleBar.getLayoutComponent(JsTitleBarLayout.CLOSE);
* </pre>
* or doing the following:
* <pre>
* JLabel icon = (JLabel) titleBar.getLayoutComponent(JsTitleBarLayout.ICON);
* JLabel title = (JLabel) titleBar.getLayoutComponent(JsTitleBarLayout.TITLE);
* JButton minimize = (JButton) titleBar.getLayoutComponent(JsTitleBarLayout.MINIMIZE);
* JButton maximize = (JButton) titleBar.getLayoutComponent(JsTitleBarLayout.MAXIMIZE_RESTORE);
* JButton close = (JButton) titleBar.getLayoutComponent(JsTitleBarLayout.CLOSE);
* </pre>
*
* @see JsTitleBarLayout
*
* @version 1.0 01/05/09
* @author Jason (MSN:www.jason0086.com@hotmail.com)
*/
public class JsTitleBar extends JsComponent implements MouseListener, MouseMotionListener {
/**
* The none button constriant.
*/
public static final int NONE_BUTTON = 0;
/**
* The all button constraint.
*/
public static final int ALL_BUTTON = 1;
/**
* The minimize button constraint.
*/
public static final int MINIMIZE_BUTTON = 2;
/**
* The maximize restore button constraint.
*/
public static final int MAXIMIZE_RESTORE_BUTTON = 3;
/**
* The close button constraint.
*/
public static final int CLOSE_BUTTON = 4;
private static final long serialVersionUID = 1822916850358931546L;
/**
* Constant to specify the js title bar layout for this title bar.
*/
private JsTitleBarLayout layoutJtb = null;
/**
* Constant to specify the root frame where this title bar lays.
*/
private Frame rootFrm = null;
/**
* Constant to specify the mouse point.
*/
private Point mousePnt = null;
/**
* Constant to specify the movabled state of this title bar,
* which also specifies the movabled state of the corresponding root frame.
*/
private boolean movableBln = true;
/**
* Constant to specify the dragging state of the mouse.
*/
private boolean draggingBln = false;
/**
* The icon for the maximize-restore button when the state of the frame is not <code>MAXIMIZED_BOTH</code>.
*/
private Icon maximizeIcn = null;
/**
* The string for the maximize-restore button when the state of the frame is not <code>MAXIMIZED_BOTH</code>.
*/
private String maximizeStr = null;
/**
* The icon for the maximize-restore button when the state of the frame is <code>MAXIMIZED_BOTH</code>.
*/
private Icon restoreIcn = null;
/**
* The string for the maximize-restore button when the state of the frame is <code>MAXIMIZED_BOTH</code>.
*/
private String restoreStr = null;
/**
* Constructs a js title bar for the specify frame.
* @param rootFrame the root frame where this title bar is to be laid
*/
public JsTitleBar(Frame rootFrame) {
this.rootFrm = rootFrame;
// set the layout
this.layoutJtb = new JsTitleBarLayout(3);
this.setLayout(this.layoutJtb);
// setup all the button
this.setupButton(ALL_BUTTON);
// add the mouse listener
this.addMouseListener(this);
// add the mouse motion listener
this.addMouseMotionListener(this);
// add the window state listner
this.rootFrm.addWindowStateListener(
new WindowStateListener() {
public void windowStateChanged(WindowEvent e) {
int newStateInt = e.getNewState();
if (newStateInt == Frame.MAXIMIZED_BOTH) {
movableBln = false;
JButton maximizeRestoreJbt = (JButton) layoutJtb.getLayoutComponent(JsTitleBarLayout.MAXIMIZE_RESTORE);
maximizeRestoreJbt.setIcon(UIManager.getIcon("InternalFrame.minimizeIcon"));
maximizeRestoreJbt.setToolTipText(UIManager.getString("InternalFrameTitlePane.restoreButtonText"));
}
else if (newStateInt != Frame.ICONIFIED) {
movableBln = true;
JButton maximizeRestoreJbt = (JButton) layoutJtb.getLayoutComponent(JsTitleBarLayout.MAXIMIZE_RESTORE);
maximizeRestoreJbt.setIcon(UIManager.getIcon("InternalFrame.maximizeIcon"));
maximizeRestoreJbt.setToolTipText(UIManager.getString("InternalFrameTitlePane.maximizeButtonText"));
}
}
}
);
}
@Override
public LayoutManager getLayout() {
return this.layoutJtb;
}
/**
* Change the icon and string associated with the look and feel.
*/
public void changeLookAndFeel() {
// minimize button
JButton minimizeJbt = (JButton) this.layoutJtb.getLayoutComponent(JsTitleBarLayout.MINIMIZE);
minimizeJbt.setIcon(UIManager.getIcon("InternalFrame.iconifyIcon"));
minimizeJbt.setToolTipText(UIManager.getString("InternalFrameTitlePane.minimizeButtonText"));
// maximize-restore button
this.maximizeIcn = UIManager.getIcon("InternalFrame.maximizeIcon");
this.maximizeStr = UIManager.getString("InternalFrameTitlePane.maximizeButtonText");
this.restoreIcn = UIManager.getIcon("InternalFrame.minimizeIcon");
this.restoreStr = UIManager.getString("InternalFrameTitlePane.restoreButtonText");
JButton maximizeRestoreJbt = (JButton) this.layoutJtb.getLayoutComponent(JsTitleBarLayout.MAXIMIZE_RESTORE);
if (this.rootFrm.getExtendedState() == Frame.MAXIMIZED_BOTH) {
maximizeRestoreJbt.setIcon(this.restoreIcn);
maximizeRestoreJbt.setToolTipText(this.restoreStr);
}
else {
maximizeRestoreJbt.setIcon(this.maximizeIcn);
maximizeRestoreJbt.setToolTipText(this.maximizeStr);
}
// close button
JButton closeJbt = (JButton) this.layoutJtb.getLayoutComponent(JsTitleBarLayout.CLOSE);
closeJbt.setIcon(UIManager.getIcon("InternalFrame.closeIcon"));
closeJbt.setToolTipText(UIManager.getString("InternalFrameTitlePane.closeButtonText"));
}
/**
* Sets the image to be desplayed in the <code>ICON</code> portion of this title bar,
* and the iconfield icon.
* @param image the image for icon
*/
public void setIconImage(Image image) {
JLabel iconJlb = new JLabel(new ImageIcon(image));
iconJlb.setPreferredSize(new Dimension(25, 25));
this.add(iconJlb, JsTitleBarLayout.ICON);
}
/**
* Sets the title text to the specify string, which is to be displayed in the
* <code>TITLE</code> portion of this title bar, and the iconfield text.
* @param title the title text. A <code>null</code> value is treated as an empty string, ""
*/
public void setTitle(String title) {
if (title == null) {
title = "";
}
JLabel titleJlb = new JLabel(title);
titleJlb.setFont(new Font(null, Font.BOLD, 16));
this.add(titleJlb, JsTitleBarLayout.TITLE);
}
/**
* Sets the enabled state of the specify button.
* @param button the button combination of this title bar associated with one of the following constraints:
* <code>NONE_BUTTON</code>, <code>ALL_BUTTON</code>, <code>MINIMIZE_BUTTON</code>,
* <code>MAXIMIZE_RESTORE_BUTTON</code>, <code>CLOSE_BUTTON</code>
* @param enabled the enabled state of the given button
*/
public void setButtonEnabled(int button, boolean enabled) {
if (button == ALL_BUTTON) {
this.setButtonEnabled(MINIMIZE_BUTTON, enabled);
this.setButtonEnabled(MAXIMIZE_RESTORE_BUTTON, enabled);
this.setButtonEnabled(CLOSE_BUTTON, enabled);
}
else if (button == MINIMIZE_BUTTON) {
this.layoutJtb.getLayoutComponent(JsTitleBarLayout.MINIMIZE).setEnabled(enabled);
}
else if (button == MAXIMIZE_RESTORE_BUTTON) {
this.layoutJtb.getLayoutComponent(JsTitleBarLayout.MAXIMIZE_RESTORE).setEnabled(enabled);
}
else if (button == CLOSE_BUTTON) {
this.layoutJtb.getLayoutComponent(JsTitleBarLayout.CLOSE).setEnabled(enabled);
}
else {
throw new IllegalArgumentException("Invalid buttonInt.");
}
}
/**
* Sets the movabled state of this title bar, which also specifies the movabled state of
* the corresponding root frame.
* @param movabled the movabled state of the root frame
*/
public void setMovable(boolean movabled) {
this.movableBln = movabled;
}
/**
* Implements <code>java.awt.event.MouseListener</code>.
*/
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
/**
* Implements <code>java.awt.event.MouseListener</code>.
*/
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
/**
* Implements <code>java.awt.event.MouseListener</code>.
*/
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
/**
* Implements <code>java.awt.event.MouseListener</code>.
*/
public void mousePressed(MouseEvent e) {
if (e.getClickCount() == 2) {
this.draggingBln = false;
if (rootFrm.isResizable()) {
if (rootFrm.getExtendedState() == Frame.MAXIMIZED_BOTH) {
rootFrm.setExtendedState(Frame.NORMAL);
}
else {
rootFrm.setExtendedState(Frame.MAXIMIZED_BOTH);
}
}
}
else if (this.movableBln) {
this.draggingBln = true;
this.mousePnt = MouseInfo.getPointerInfo().getLocation();
}
}
/**
* Implements <code>java.awt.event.MouseListener</code>.
*/
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
/**
* Implements <code>java.awt.event.MouseMotionListener</code>.
*/
public void mouseDragged(MouseEvent e) {
if (this.draggingBln
&& this.movableBln) {
Point xPnt = MouseInfo.getPointerInfo().getLocation();
int x = this.rootFrm.getX();
int y = this.rootFrm.getY();
x += xPnt.x - this.mousePnt.x;
y += xPnt.y - this.mousePnt.y;
this.mousePnt = xPnt;
this.rootFrm.setLocation(x, y);
}
}
/**
* Implements <code>java.awt.event.MouseMotionListener</code>.
*/
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
private void setupButton(int buttonInt) {
if (buttonInt == NONE_BUTTON) {
Component xCmp = null;
if ((xCmp = this.layoutJtb.getLayoutComponent(JsTitleBarLayout.MINIMIZE)) != null) {
this.layoutJtb.removeLayoutComponent(xCmp);
}
if ((xCmp = this.layoutJtb.getLayoutComponent(JsTitleBarLayout.MAXIMIZE_RESTORE)) != null) {
this.layoutJtb.removeLayoutComponent(xCmp);
}
if ((xCmp = this.layoutJtb.getLayoutComponent(JsTitleBarLayout.CLOSE)) != null) {
this.layoutJtb.removeLayoutComponent(xCmp);
}
}
else if (buttonInt == ALL_BUTTON) {
this.setupButton(MINIMIZE_BUTTON);
this.setupButton(MAXIMIZE_RESTORE_BUTTON);
this.setupButton(CLOSE_BUTTON);
}
else if (buttonInt == MINIMIZE_BUTTON) {
JButton minimizeJbt = new JButton();
minimizeJbt.setIcon(UIManager.getIcon("InternalFrame.iconifyIcon"));
minimizeJbt.setToolTipText(UIManager.getString("InternalFrameTitlePane.minimizeButtonText"));
minimizeJbt.setPreferredSize(new Dimension(25, 25));
minimizeJbt.addMouseListener(
new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
rootFrm.setExtendedState(Frame.ICONIFIED);
}
}
);
this.add(minimizeJbt, JsTitleBarLayout.MINIMIZE);
}
else if (buttonInt == MAXIMIZE_RESTORE_BUTTON) {
JButton maximizeRestoreJbt = new JButton();
if (this.rootFrm.getExtendedState() == Frame.MAXIMIZED_BOTH) {
maximizeRestoreJbt.setIcon(UIManager.getIcon("InternalFrame.minimizeIcon"));
maximizeRestoreJbt.setToolTipText(UIManager.getString("InternalFrameTitlePane.restoreButtonText"));
}
else {
maximizeRestoreJbt.setIcon(UIManager.getIcon("InternalFrame.maximizeIcon"));
maximizeRestoreJbt.setToolTipText(UIManager.getString("InternalFrameTitlePane.maximizeButtonText"));
}
maximizeRestoreJbt.setPreferredSize(new Dimension(25, 25));
maximizeRestoreJbt.addMouseListener(
new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (rootFrm.getExtendedState() == Frame.MAXIMIZED_BOTH) {
rootFrm.setExtendedState(Frame.NORMAL);
}
else {
rootFrm.setExtendedState(Frame.MAXIMIZED_BOTH);
}
}
}
);
this.add(maximizeRestoreJbt, JsTitleBarLayout.MAXIMIZE_RESTORE);
}
else if (buttonInt == CLOSE_BUTTON) {
JButton closeJbt = new JButton();
closeJbt.setIcon(UIManager.getIcon("InternalFrame.closeIcon"));
closeJbt.setToolTipText(UIManager.getString("InternalFrameTitlePane.closeButtonText"));
closeJbt.setPreferredSize(new Dimension(25, 25));
closeJbt.addMouseListener(
new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
rootFrm.dispose();
}
}
);
this.add(closeJbt, JsTitleBarLayout.CLOSE);
}
else {
throw new IllegalArgumentException("Invalid buttonInt.");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -