package code;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
public class MainFrame extends JFrame implements ActionListener {
String userName;
String lastCard = "";
boolean bLook = false;
DefaultMutableTreeNode top = new DefaultMutableTreeNode (new MyNode("0","操作选项"));
DefaultMutableTreeNode top1 = new DefaultMutableTreeNode(new MyNode("1","系统选项"));
DefaultMutableTreeNode top2 = new DefaultMutableTreeNode(new MyNode("2","系统管理"));
DefaultMutableTreeNode top3 = new DefaultMutableTreeNode(new MyNode("3","联系人列表"));
DefaultMutableTreeNode node1_1 = new DefaultMutableTreeNode(new MyNode("11","修改密码"));
DefaultMutableTreeNode node1_2 = new DefaultMutableTreeNode(new MyNode("12","退出"));
DefaultMutableTreeNode node1_3 = new DefaultMutableTreeNode(new MyNode("13","注销"));
DefaultMutableTreeNode node2_1 = new DefaultMutableTreeNode(new MyNode("21","添加"));
DefaultMutableTreeNode node2_2 = new DefaultMutableTreeNode(new MyNode("22","修改"));
DefaultMutableTreeNode node2_3 = new DefaultMutableTreeNode(new MyNode("23","删除"));
DefaultMutableTreeNode node2_4 = new DefaultMutableTreeNode(new MyNode("24","查看"));
DefaultMutableTreeNode node3_1 = new DefaultMutableTreeNode(new MyNode("31","联系人组"));
DefaultMutableTreeNode node3_2 = new DefaultMutableTreeNode(new MyNode("32","联系人名单"));
DefaultMutableTreeNode perNode;
JButton jbSearch ;
JTextField jtSearch ;
JRadioButton r1 ;
JRadioButton r2 ;
DefaultTreeModel dtm = new DefaultTreeModel(top);
JTree jt = new JTree(dtm);
JScrollPane jspz = new JScrollPane(jt);
JPanel jpy = new JPanel();
JPanel jpSearch = new JPanel();
JSplitPane jsp0 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,jpSearch,jpy);
JSplitPane jspl = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jspz,jsp0);
CardLayout cl ;
Welcome welcome;
ChangePwd changePwd ;
Client client;
ClientList clientlist ;
public MainFrame(String userName){
this.userName = userName ;
this.initialTree();
this.initialSearch();
this.initialPanel();
this.initialJpy();
this.initialFrame();
this.addListener();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jbSearch) {
String sql ;
String s = jtSearch.getText().trim() ;
if(r2.isSelected()==true) {
sql = "select * from client where Pnumber='"+s+"'and Uid='"+userName+"'";
s = DButil.select(sql);
}
client.setText(s);
client.setEdit(false);
cl.show(jpy,"client");
}
}
public void initialTree(){
top.add(top1);
top.add(top2);
top.add(top3);
top1.add(node1_1);
top1.add(node1_3);
top1.add(node1_2);
top3.add(node3_1);
top3.add(node3_2);
top2.add(node2_1);
top2.add(node2_2);
top2.add(node2_3);
top2.add(node2_4);
// String sqlClient = "select Pname from client where Uid ='"+userName+"'ORDER BY Pname ASC";
String sqlGroup = "select DISTINCT Pgroup from client where Uid ='"+userName+"'ORDER BY Pname ASC";
// this.updateClientNode(sqlClient);
this.updateGroupNode(sqlGroup);
((DefaultTreeModel)jt.getModel()).reload(top);
for(int i=0;i<15;i++)
jt.expandRow(i);
}
/**
* 动态添加联系人列表及分组列表
*/
/* public void updateClientNode(String sqlClient){
Vector<String> vectorClient = new Vector<String>();
DButil.selectOne(sqlClient,vectorClient);
node3_2.removeAllChildren();
for(int i=0;i<vectorClient.size();i++) {
perNode = new DefaultMutableTreeNode(new MyNode("320",vectorClient.get(i)));
node3_2.add(perNode);
}
}*/
public void updateGroupNode(String sqlGroup){
Vector<String> vectorGroup = new Vector<String>();
DButil.selectOne(sqlGroup,vectorGroup);
node3_1.removeAllChildren();
for(int i=0;i<vectorGroup.size();i++) {
perNode = new DefaultMutableTreeNode(new MyNode("310",vectorGroup.get(i)));
node3_1.add(perNode);
}
}
public void initialSearch() {
jpSearch.setLayout(null);
jbSearch = new JButton("查找");
jbSearch.setBounds(10,5,60,20);
jtSearch = new JTextField();
jtSearch.setBounds(80,5,140,20);
r1 = new JRadioButton("按姓名",true);
r1.setBounds(220,5,70,20);
r2 = new JRadioButton("按号码");
r2.setBounds(290,5,80,20);
ButtonGroup bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
jpSearch.add(jbSearch);
jpSearch.add(jtSearch);
jpSearch.add(r1);
jpSearch.add(r2);
jbSearch.addActionListener(this);
}
public void initialPanel(){
changePwd = new ChangePwd(userName);
welcome = new Welcome();
client = new Client();
client.initialFrame();
clientlist = new ClientList();
clientlist.initialFrame();
}
public void initialJpy(){
jpy.setLayout(new CardLayout());
cl = (CardLayout)jpy.getLayout();
jpy.add(welcome,"welcome");
jpy.add(changePwd,"changePwd");
jpy.add(client,"client");
jpy.add(clientlist,"clientlist");
/*****************待加*******/
}
public void initialFrame(){
this.add(jspl);
jsp0.setDividerLocation(35);
jsp0.setDividerSize(3);
jspl.setDividerLocation(130);
jspl.setDividerSize(2);
Image image = new ImageIcon("E:\\javacode\\workspace\\BBS\\WebRoot\\images\\avatar-display.png").getImage();
this.setIconImage(image);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize() ;
int centerX = screenSize.width/2 ;
int centerY = screenSize.height/2 ;
int w = 500 ;
int h = 550 ;
this.setBounds(centerX-w/2,centerY-h/2,w,h);
this.setTitle(userName+" 的通讯录");
this.setVisible(true);
this.setResizable(false);
}
public void addListener(){
jt.addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e){
DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode)jt.getLastSelectedPathComponent();
MyNode myNode = (MyNode) dmtn.getUserObject();
String id = myNode.getId();
String name = myNode.getName() ;
if(id.equals("0")) { //欢迎界面
cl.show(jpy,"welcome");
}
else if(id.equals("11")) { //改密
cl.show(jpy,"changePwd");
}
else if(id.equals("12")){ //退出
int i = JOptionPane.showConfirmDialog(jpy,"您确定要退出?","询问",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(i == 0){
System.exit(0);
}
}
else if(id.equals("13")) {
int i = JOptionPane.showConfirmDialog(jpy,"您确定要注销账户?","询问",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(i == 0){
String sql = "delete from user where uid='"+userName+"'";
String sql2 = "delete from client where Uid='"+userName+"'";
int count2 = DButil.update(sql2);
int count1 = DButil.update(sql);
if(count1>0) {
JOptionPane.showMessageDialog(jpy,"注销成功!","提示",JOptionPane.INFORMATION_MESSAGE);
System.out.println(count1+count2);
System.exit(0);
}
}
}
else if(id.equals("310")){ //分组
clientlist.setText(name);
clientlist.setEdit(false);
cl.show(jpy,"clientlist");
}
/* else if(id.equals("320")){ //联系人
client.setText(name) ;
client.setEdit(false);
cl.show(jpy,"client");
}*/
else if(id.equals("22")){ //修改
if(/*getLastCard().equals("320")||*/getLastCard().equals("24")&&bLook) {
client.s