📄 slithermenu.java
字号:
**=======================================================================**
*/
public void initButtonPanelDimension() {
for (int i = 0; i < panelconut; i++) {
((JPanel)template.get(i)).setBounds(sm_X, sm_Y, slitherMenuBar_Width,
slitherMenuBar_Height - titleHeight * (panelconut - 1));
}//Endfor
}
/**=======================================================================**
* [## public void setInitMenu() {} ]:
* 参数 :无
* 返回值 :无
* 修饰符 :public
* 功能 :设置初始化后展开第一项菜单(在主窗口的setVisible()方法之后使用)
**=======================================================================**
*/
public void setInitMenu() {
if(panelconut > 0) {
JPanel mainMenu = (JPanel)template.get(0); //展开第一项菜单
int titlesHeight = (panelconut - 1) * titleHeight; //获得其它模板标题的高度总和
int height = slitherMenuBar_Height - titlesHeight;
mainMenu.setBounds(sm_X, sm_Y, slitherMenuBar_Width, height);
//处理其它菜单标题
for (int i = 1; i < panelconut; i++) {
((JPanel)template.get(i)).setBounds(sm_X, sm_Y + height + (i - 1) * titleHeight,
slitherMenuBar_Width, titleHeight);
}//Endfor
}else {
String msg = "没有按键模板可组织。";
JOptionPane.showMessageDialog(null, msg, "错误",JOptionPane.ERROR_MESSAGE);
}//Endif
}
/**=======================================================================**
* [## public void setMenuDimension(int w,int h) {} ]:
* 参数 :int w 表示菜单宽度,int h 表示菜单高度
* 返回值 :无
* 修饰符 :public
* 功能 :设置菜单宽度和高度(在初始化菜单后加入组件之前使用)
**=======================================================================**
*/
public void setMenuDimension(int w,int h) {
if(panelconut == 0) {
slitherMenuBar_Width = w;
slitherMenuBar_Height = h;
}else {
String msg = "setMenuDimension()方法请在加入组件之前使用。";
JOptionPane.showMessageDialog(null, msg, "警告",JOptionPane.WARNING_MESSAGE);
}//Endif
}
/**=======================================================================**
* [## public void setMenuLocation(int x, int y) {} ]:
* 参数 :int x 表示菜单的横向坐标,int y 表示菜单的纵向坐标
* 返回值 :无
* 修饰符 :public
* 功能 :设置菜单的坐标(在初始化菜单后加入组件之前使用)
**=======================================================================**
*/
public void setMenuLocation(int x, int y) {
if(panelconut == 0) {
sm_X = x;
sm_Y = y;
}else {
String msg = "setMenuLocation()方法请在加入组件之前使用。";
JOptionPane.showMessageDialog(null, msg, "警告",JOptionPane.WARNING_MESSAGE);
}//Endif
}
/**=======================================================================**
* [## public void setTitleHeight(int h) {} ]:
* 参数 :int h 表示按键模板的标题键的高度
* 返回值 :无
* 修饰符 :public
* 功能 :设置模板标题按键高度(当标题按键有背景图标时才使用本方法)
**=======================================================================**
*/
public void setTitleHeight(int h) {
titleHeight = h;
}
/**=======================================================================**
* [## public void setButtonPanelBackground(Color bg) {} ]:
* 参数 :Color 对象表示按键模板的背景颜色
* 返回值 :无
* 修饰符 :public
* 功能 :设置按键模板背景颜色(在初始化菜单后加入组件之后使用)
**=======================================================================**
*/
public void setButtonPanelBackground(Color bg) {
if(panelconut > 0) {
for (int i = 0; i < panelconut; i++) {
((JPanel)buttonPanels.get(i)).setBackground(bg);
}//Endfor
}else {
String msg = "setButtonPanelBackground()方法请在加入组件之后使用。";
JOptionPane.showMessageDialog(null, msg, "警告",JOptionPane.WARNING_MESSAGE);
}//Endif
}
/**=======================================================================**
* [## public String getSelectButtonName() {} ]:
* 参数 :无
* 返回值 :String 对象表示触发事件的功能按键的名字
* 修饰符 :public
* 功能 :获得选中的按键名
**=======================================================================**
*/
public String getSelectButtonName() {
return selectButtonName;
}
/**=======================================================================**
* [## private void slither(int index) {} ]:
* 参数 :无
* 返回值 :无
* 修饰符 :private
* 功能 :处理菜单滑动效果
**=======================================================================**
*/
private void slither(int index) {
//获得其它标题的高度总和
int sp_h = slitherMenuBar_Height - titleHeight * (panelconut - 1);
if(index == selectPanelNumber) { //如果是当前面板,则不处理
return;
}else if(index > selectPanelNumber) { //菜单上移动作
int sp_y = titleHeight * (selectPanelNumber + 1);
for (int i = sp_h; i >= titleHeight; i --) {
//当前展开面板缩起
((JPanel)template.get(selectPanelNumber)).setSize(slitherMenuBar_Width, i);
//处理当前展开面板与将要展开面板之间的标题
for (int j = selectPanelNumber + 1; j < index; j++) {
int other_Y = ((JPanel)template.get(j)).getY() - 1;
((JPanel)template.get(j)).setLocation(sm_X, other_Y);
}//Endfor
//新面板展开
int index_Y = ((JPanel)template.get(index)).getY() - 1;
int index_H = ((JPanel)template.get(index)).getHeight() + 1;
((JPanel)template.get(index)).setBounds(sm_X, index_Y, slitherMenuBar_Width, index_H);
}//Endfor
}else if(index < selectPanelNumber) { //下移动作
int sp_y = titleHeight * (selectPanelNumber - 2);
for (int i = sp_h; i >= titleHeight; i --) {
//当前展开面板缩起
int spy = ((JPanel)template.get(selectPanelNumber)).getY() + 1;
((JPanel)template.get(selectPanelNumber)).setBounds(sm_X, spy, slitherMenuBar_Width, i);
//处理当前展开面板与将要展开面板之间的标题
for (int j = selectPanelNumber - 1; j > index; j--) {
int other_Y = ((JPanel)template.get(j)).getY() + 1;
((JPanel)template.get(j)).setLocation(sm_X, other_Y);
}//Endfor
//新面板展开
int index_H = ((JPanel)template.get(index)).getHeight() + 1;
((JPanel)template.get(index)).setSize(slitherMenuBar_Width, index_H);
}//Endfor
}//Endif
this.validate(); //确定当前菜单的变化
}
/**=======================================================================**
* ActionListener 监听
**=======================================================================**
*/
public void actionPerformed ( ActionEvent ae ) {
//获得触发事件的按键Name
selectButtonName = ((JButton)ae.getSource()).getName();
if(selectButtonName != null) { //不为空则选择的就是标题按键
//获得被选中模板的序号
selectPanelNumberNew = Integer.parseInt(selectButtonName);
//处理菜单滑动效果
slither(selectPanelNumberNew);
selectPanelNumber = selectPanelNumberNew;
}
else {
//获得功能按键的名字
selectButtonName = ((JButton)ae.getSource()).getText();
}//Endif
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -