?? navpanel.java
字號:
/* * @(#) NAVPanel.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.panel.navbar;//導入核心Java類庫import java.awt.Font;import java.awt.Graphics;import java.awt.Rectangle;import java.awt.FontMetrics;import java.awt.Image;import java.awt.Point;import java.awt.Button;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.util.List;import java.util.ArrayList;import javax.swing.JPanel;//導入自定義Java類庫import hws.item.smart.misc.ColorShop;import hws.item.smart.misc.ImageShop;/** * 導航面板 * * @version 0.1 2005-08-07 * @author Hwerz */public class NAVPanel extends JPanel implements ActionListener, MouseListener, MouseMotionListener { /*------------------------------------------------------------------------* * 屬性定義 * *------------------------------------------------------------------------*/ /** * 按鈕標簽的字體 */ public static final Font BUTTON_FONT = new Font("宋體", Font.PLAIN, 12); /** * 導航面板容納按鈕的總數 */ private static final int BUTTON_COUNT = 20; /** * 導航面板上的父按鈕數組 */ private Button parentButtons[]; /** * 已經添加到面板上的父按鈕個數 */ private int numberOfParentButton; /** * 被選中父按鈕的索引 */ private int selectedParentButtonIndex; /** * 父按鈕的高度 */ private int heightOfParentButton; /** * 每個父按鈕里面所包括的子按鈕 */ private Image sonButtons[][]; /** * 子按鈕的標簽 */ private String sonButtonLabels[][]; /** * 每個組里子按鈕的個數 */ private int numberOfSonButton[]; /** * 子按鈕所占面板寬度的比例 */ private int widthPercentOfSonButton; /** * 子按鈕之間的間距 */ private int deltaBetweenSonButtons; /** * 第一個被顯示的子按鈕 */ private int theFirstDisplaySonButton; /** * 被按下的子按鈕 */ private int pressedSonButton; /** * 單擊則向上滾動的帶箭頭小按鈕 */ private Image slideUpButton; /** * 單擊則向下滾動的帶箭頭小按鈕 */ private Image slideDownButton; /** * 是否顯示slideDownButton */ private boolean showSlideDownButton; /** * 子按鈕的滾動次數 */ private int slideStep; /** * 子按鈕的滾動節奏 */ private int slideIndex; /** * 記錄鼠標的當前位置 */ private Point mousePoint; /** * 事件監聽器集合 */ private List listeners; /*------------------------------------------------------------------------* * 構造函數 * *------------------------------------------------------------------------*/ /** * Create a new instance of this class */ public NAVPanel() { super(); parentButtons = new Button[BUTTON_COUNT]; numberOfParentButton = 0; selectedParentButtonIndex = -1; heightOfParentButton = 25; sonButtons = new Image[BUTTON_COUNT][BUTTON_COUNT]; sonButtonLabels = new String[BUTTON_COUNT][BUTTON_COUNT]; numberOfSonButton = new int[BUTTON_COUNT]; widthPercentOfSonButton = 40; deltaBetweenSonButtons = 20; theFirstDisplaySonButton = -1; pressedSonButton = -1; slideUpButton = ImageShop.UP_IMAGEICON.getImage(); slideDownButton = ImageShop.DOWN_IMAGEICON.getImage(); slideStep = 3; showSlideDownButton = false; slideIndex = 0; mousePoint = null; listeners = new ArrayList(); setLayout(null); setBackground(ColorShop.NAVPANEL_BG_COLOR); addMouseMotionListener(this); addMouseListener(this); } /*------------------------------------------------------------------------* * 公共方法 * *------------------------------------------------------------------------*/ /** * 添加SonButtonClickedListener事件監聽器 * * @param listener 待添加的SonButtonClickedListener */ public synchronized void addSonButtonClickedListener( SonButtonClickedListener listener) { listeners.add(listener); } /** * 刪除SonButtonClickedListener事件監聽器 * * @param listener 待刪除的SonButtonClickedListener */ public synchronized void removeSonButtonClickedListener( SonButtonClickedListener listener) { listeners.remove(listener); } /** * 返回父按鈕的個數 * * @return 父按鈕的個數 */ public int getNumberOfParentButton() { return numberOfParentButton; } /** * 返回指定索引的父按鈕對象 * * @param index 父按鈕的索引 * @return 指定索引的父按鈕對象 */ public Button getParentButton(int index) { Button parentButton = null; if (index >= 0 && index < numberOfParentButton) { parentButton = parentButtons[index]; } return parentButton; } /** * 添加父按鈕 * * @param text 待添加父按鈕的文本 * @return 如果添加成功則返回true,否則返回false */ public boolean addParentButton(String text) { boolean success = false; int i = numberOfParentButton; if (i < BUTTON_COUNT) { Button button = new Button(text); parentButtons[i] = button; numberOfSonButton[i] = 0; numberOfParentButton++; add(button); button.addActionListener(this); setSelectedParentButton(i); success = true; } return success; } /** * 向指定的父按鈕所在的組里添加子按鈕 * * @param index 指定父按鈕的索引 * @param image 待添加子按鈕的圖標 * @param text 待添加子按鈕的文本 */ public void addSonToParent(int index, Image image, String text) { if (index < numberOfParentButton && numberOfSonButton[index] < BUTTON_COUNT) { sonButtons[index][numberOfSonButton[index]] = image; sonButtonLabels[index][numberOfSonButton[index]] = text; numberOfSonButton[index]++; repaint(); } } /** * 選中指定索引的父按鈕 * * @param index 父按鈕的索引 */ public void setSelectedParentButton(int index) { if (index >= 0 && index < numberOfParentButton) { if (selectedParentButtonIndex != index) { selectedParentButtonIndex = index; theFirstDisplaySonButton = 0; repaint(); } } } /*------------------------------------------------------------------------* * 私有方法 * *------------------------------------------------------------------------*/ /** * 返回子按鈕的寬度(注意:所有子按鈕的寬度都一致) * * @return 子按鈕的寬度 */ private int getSonButtonWidth() { return (widthPercentOfSonButton * getWidth()) / 100; } /** * 返回指定子按鈕的邊界(注意:這個子按鈕是隸屬于當前選定的父按鈕) * * @param index 子按鈕的索引號 * @return 子按鈕的邊界 */ private Rectangle getSonButtonRectangle(int index) { int x = (getWidth() - getSonButtonWidth()) / 2; int y = (selectedParentButtonIndex + 1) * heightOfParentButton + (index - theFirstDisplaySonButton) * (getSonButtonWidth() + deltaBetweenSonButtons) + deltaBetweenSonButtons / 2; Rectangle rectangle = new Rectangle(x, y, getSonButtonWidth(), getSonButtonWidth()); return rectangle; } /** * 返回slideDownButton的邊界 * * @return slideDownButton的邊界 */ private Rectangle getSlideDownButtonRect() { int y = getHeight() - (numberOfParentButton - selectedParentButtonIndex) * heightOfParentButton; Rectangle rectangle = new Rectangle(getWidth() - 20, y, 16, 16); return rectangle; } /** * 返回slideUpButton的邊界 * * @return slideUpButton的邊界 */ private Rectangle getSlideUpButtonRect() { int y = (selectedParentButtonIndex + 1) * heightOfParentButton + 16; Rectangle rectangle = new Rectangle(getWidth() - 20, y, 16, 16); return rectangle; } /** * 返回指定位置處按鈕的索引如果是父按鈕,則返回值為索引的相反數減去2;如果是子按 * 鈕,則返回索引值 * * @param point 指定的位置 * @return 指定位置處按鈕的索引 */ private int getButtonIndex(Point point) { int index = -1; if (point != null) { if (numberOfParentButton != 0) { for (int i = 0; i < numberOfParentButton; i++) { Rectangle rectangle = getParentButton(i).getBounds(); //鼠標在某一父按鈕的上面 if (rectangle.contains(point)) { index = -i - 2; break; } } if (index == -1) { for (int i = theFirstDisplaySonButton; i < theFirstDisplaySonButton + numberOfSonButton[selectedParentButtonIndex]; i++) { Rectangle rectangle = getSonButtonRectangle(i); //鼠標在某一子按鈕的上面 if (rectangle.contains(point)) { index = i; break; } } } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -