📄 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 + -