package view.jpanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import action.JGroupPanelAction;
/**
* 类似QQ界面的组群管理面板
*
* @author Administrator
*
*/
public class JGroupPanel extends JPanel {
/* 用来管理组的三个容器 */
private JPanel pNorth = new JPanel() {
};
private JGroupPanelAction action=new JGroupPanelAction();
private JPanel pCenter = new JPanel();
private JPanel pSouth = new JPanel();
/* 当前全部组的集合 */
private ArrayList groupList = new ArrayList();
/* 是否已禁止添加组件 */
private boolean forbidFlag = false;
/* 当前激活的组 */
private JGroupContainer activeGroup = null;
transient ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton bttTitle = (JButton) e.getSource();
expandGroup((JGroupContainer) bttTitle.getParent());
}
};
private boolean hasCreateDefaultGroup = false;
public JGroupPanel() {
initComponents();
createDefaultGroup();
}
private void initComponents() {
this.setLayout(new BorderLayout());
this.add(pNorth, BorderLayout.NORTH);
this.add(pCenter, BorderLayout.CENTER);
this.add(pSouth, BorderLayout.SOUTH);
pNorth.setLayout(new GroupLayout());
pCenter.setLayout(new BorderLayout());
pSouth.setLayout(new GroupLayout());
forbidFlag = true;
}
private void createDefaultGroup() {
insertGroup(0, "学生管理");
addMember(0, buildLabel("添加学生信息"));
addMember(0, buildLabel("修改学生信息"));
addMember(0, buildLabel("删除学生信息"));
getGroup(0).setMemberGap(10, 5);
insertGroup(1, "老师管理");
addMember(1, buildLabel("添加老师信息"));
addMember(1, buildLabel("修改老师信息"));
addMember(1, buildLabel("删除老师信息"));
getGroup(1).setMemberGap(10, 5);
insertGroup(2, "院长管理");
addMember(2, buildLabel("添加院长信息"));
addMember(2, buildLabel("修改院长信息"));
addMember(2, buildLabel("删除院长信息"));
getGroup(2).setMemberGap(10, 5);
expandGroup(1);
hasCreateDefaultGroup = true;
}
/**
* 构造工具栏按钮的公用方法
*
* @return
*/
public JLabel buildLabel(String name) {
JLabel button = new JLabel(name);
button.setToolTipText(name);
// button.setRolloverEnabled(true);
// button.setBorderPainted(true);
button.setVerticalTextPosition(JButton.BOTTOM);
button.setHorizontalTextPosition(JButton.CENTER);
// 添加按钮事件
// button.addActionListener(action);
return button;
}
/**
* 展开组
*
* @param name
* String 组名
*/
public void expandGroup(String name) {
for (int i = getGroupCount() - 1; i >= 0; i--) {
if (getGroupName(i).equals(name)) {
expandGroup(i);
}
}
}
/**
* 展开组
*
* @param index
* int 组的顺序号
*/
public void expandGroup(int index) {
expandGroup(getGroup(index));
}
/**
* 展开组
*
* @param group
* JGroupContainer 组
*/
protected void expandGroup(JGroupContainer group) {
pNorth.removeAll();
pCenter.removeAll();
pSouth.removeAll();
boolean hasAddCenter = false;
for (int i = 0; i < groupList.size(); i++) {
Component c = (Component) groupList.get(i);
if (hasAddCenter) {
pSouth.add(c);
} else if (c == group) {
pCenter.add(c, BorderLayout.CENTER);
hasAddCenter = true;
} else {
pNorth.add(c);
}
}
if (activeGroup != null) {
activeGroup.collapse();
}
activeGroup = group;
activeGroup.expand();
pNorth.doLayout();
pCenter.doLayout();
pSouth.doLayout();
doLayout();
}
/**
* 收缩组
*
* @param name
* String 组名
*/
public void collapseGroup(String name) {
for (int i = getGroupCount() - 1; i >= 0; i--) {
if (getGroupName(i).equals(name)) {
collapseGroup(i);
}
}
}
/**
* 收缩组
*
* @param index
* int 组的顺序号
*/
public void collapseGroup(int index) {
collapseGroup(getGroup(index));
}
/**
* 收缩组
*
* @param group
* JGroupContainer 组
*/
protected void collapseGroup(JGroupContainer group) {
if (group == activeGroup) {
activeGroup.collapse();
activeGroup = null;
}
}
/**
* 添加组
*
* @param name
* String 组名
*/
public void addGroup(String name) {
this.insertGroup(getGroupCount(), name);
}
/**
* 插入一个组
*
* @param index
* int 顺序号
* @param name
* String 组名
* @param bg
* Color 背景色
*/
public void insertGroup(int index, String name, Color bg) {
if (index < 0 || index > groupList.size()) {
throw new ArrayIndexOutOfBoundsException("index:" + index
+ " >count:" + groupList.size());
}
if (hasCreateDefaultGroup) {
while (getGroupCount() > 0) {
removeGroup(0);
}
hasCreateDefaultGroup = false;
}
int countNorth = pNorth.getComponentCount();
int countCenter = pCenter.getComponentCount();
int countSouth = pSouth.getComponentCount();
JGroupContainer group;
if (index <= countNorth) {
group = insertGroup(pNorth, index, name, bg);
} else if (index <= countNorth + countCenter) {
group = insertGroup(pCenter, index - countNorth, name, bg);
} else if (index <= countNorth + countCenter + countSouth) {
group = insertGroup(pSouth, index - countNorth - countCenter, name,
bg);
} else {
group = insertGroup(pSouth, countSouth, name, bg);
}
group.getTitleButton().addActionListener(al);
groupList.add(index, group);
}
/**
* 插入一个组
*
* @param index
* int 顺序号
* @param name
* String 组名
*/
public void insertGroup(int index, String name) {
insertGroup(index, name, UIManager.getColor("Desktop.background"));
}
/**
* 插入一个组
*
* @param p
* JPanel 目标面板
* @param index
* int 顺序号
* @param name
* String 组名
*
*
* /** 插入一个组
* @param p
* JPanel 目标面板
* @param index
* int 顺序号
* @param name
* String 组名
* @return JGroupContainer
*/
private JGroupContainer insertGroup(JPanel p, int index, String name,
Color bg) {
JGroupContainer group = new JGroupContainer(name, bg);
p.add(group);
return group;
}
/**
* 删除一个组
*
* @param index
* int 顺序号
*/
public void removeGroup(int index) {
JGroupContainer c = (JGroupContainer) groupList.get(index);
c.getParent().remove(c);
c.getTitleButton().removeActionListener(al);
}
/**
* 删除一个组
*
* @param name
* String 组名
*/
public void removeGroup(String name) {
for (int i = getGroupCount() - 1; i >= 0; i--) {
if (getGroupName(i).equals(name)) {
this.removeGroup(i);
}
}
}
/**
* 设置组名
*
* @param index
* int 顺序号
* @param name
* String 组名
*/
public void setGroupName(int index, String name) {
this.getGroup(index).setName(name);
}
/**
* 取得组名
*
* @param groupIndex
* int 顺序号
* @return String 组名
*/
public String getGroupName(int groupIndex) {
return getGroup(groupIndex).getName();
}
/**
* 取得全部组名
*
* @return String[]
*/
public String[] getGroupNames() {
String sResult[] = new String[getGroupCount()
java小系统,学生信息管理系统,java源码,实例
5星 · 超过95%的资源 需积分: 34 15 浏览量
2009-08-10
10:33:47
上传
评论 3
收藏 15KB RAR 举报
tsoorr
- 粉丝: 1
- 资源: 8