仿QQjava源代码.docx
《仿QQjava源代码.docx》由会员分享,可在线阅读,更多相关《仿QQjava源代码.docx(37页珍藏版)》请在冰豆网上搜索。
仿QQjava源代码
importjava.awt.BorderLayout;
importjava.awt.Component;
importjava.awt.Dialog;
importjava.awt.Dimension;
importjava.awt.FlowLayout;
importjava.awt.GridBagConstraints;
importjava.awt.GridBagLayout;
importjava.awt.GridLayout;
importjava.awt.Image;
importjava.awt.Insets;
importjava.awt.MenuItem;
importjava.awt.PopupMenu;
importjava.awt.Rectangle;
importjava.awt.SystemTray;
importjava.awt.Toolkit;
importjava.awt.TrayIcon;
importjava.awt.TrayIcon.MessageType;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.ComponentAdapter;
importjava.awt.event.ComponentEvent;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
import.DatagramPacket;
import.DatagramSocket;
import.InetAddress;
import.SocketException;
import.UnknownHostException;
importjava.util.Date;
importjava.util.Scanner;
importjava.util.Stack;
importjava.util.prefs.Preferences;
importjavax.swing.BoxLayout;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JDialog;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JList;
importjavax.swing.JMenuItem;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JPasswordField;
importjavax.swing.JPopupMenu;
importjavax.swing.JProgressBar;
importjavax.swing.JScrollPane;
importjavax.swing.JTabbedPane;
importjavax.swing.JTextField;
importjavax.swing.JToggleButton;
importjavax.swing.ScrollPaneConstants;
importjavax.swing.SwingConstants;
importjavax.swing.UIManager;
importjavax.swing.border.BevelBorder;
importjavax.swing.border.Border;
importjavax.swing.border.EmptyBorder;
importjavax.swing.border.TitledBorder;
importjavax.swing.tree.DefaultMutableTreeNode;
importjavax.swing.tree.DefaultTreeModel;
importjavax.swing.tree.TreePath;
publicclassEQextendsJDialog{
privateJTextFieldipEndTField;
privateJTextFieldipStartTField;
privateJTextFielduserNameTField;//用户框
privateJPasswordFieldpasswordTField;//密码框
privateJTextFieldplacardPathTField;//placard:
公告
privateJTextFieldupdatePathTField;//更新路径
privateJTextFieldpubPathTField;
publicstaticEQframe=null;
privateChatTreechatTree;
privateJPopupMenupopupMenu;//右键菜单
privateJTabbedPanetabbedPane;//分页面板
privateJToggleButtonsearchUserButton;//搜索用户按钮
privateJProgressBarprogressBar;//滚动条
privateJListfaceList;
privateJButtonselectInterfaceOKButton;
privateDatagramSocketss;//数据报插座
privatefinalJLabelstateLabel;
privatestaticStringuser_dir;
privatestaticFilelocalFile;
privatestaticFilenetFile;
privateStringnetFilePath;
privateJButtonmessageAlertButton;
privateStackmessageStack;////Stack类表示后进先出(LIFO)的对象堆栈。
privateImageIconmessageAlertIcon;
privateImageIconmessageAlertNullIcon;
privateRectanglelocation;
publicstaticTrayIcontrayicon;
privateDaodao;
//Preferences.systeRoot:
返回系统的根首选项节点 Preferences:
参数选择
publicfinalstaticPreferencespreferences=Preferences.systemRoot();
privateJButtonuserInfoButton;
//----------------------------------------------------------------------------------
publicstaticvoidmain(String[]args){
try{
Stringlaf=preferences.get("lookAndFeel","java默认");
if(laf.indexOf("当前系统")>-1){
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
EQframe=newEQ();
frame.setVisible(true);
frame.SystemTrayInitial();//初始化系统栏
frame.server();
frame.checkPlacard();
}catch(Exceptione){
e.printStackTrace();
}
}
//----------------------------------------------------------------------------------
publicEQ(){
super(newJFrame());
frame=this;
dao=Dao.getDao();
location=dao.getLocation();
setTitle("EQ聊天");
setBounds(location);
progressBar=newJProgressBar();
//BevelBorder:
该类实现简单的双线斜面边框。
Bevel:
斜面 lowered:
凹入斜面类型。
progressBar.setBorder(newBevelBorder(BevelBorder.LOWERED));
tabbedPane=newJTabbedPane();
popupMenu=newJPopupMenu();
chatTree=newChatTree(this);
user_dir=System.getProperty("user.dir");//程序执行路径用于系统更新
localFile=newFile(user_dir+File.separator+"EQ.jar");//本地EQ文件
stateLabel=newJLabel();//状态栏标签
this.addWindowListener(newFrameWindowListener());//添加窗体监视器
this.addComponentListener(newComponentAdapter(){
publicvoidcomponentResized(finalComponentEvente){
saveLocation();
}
publicvoidcomponentMoved(finalComponentEvente){
saveLocation();
}
});
try{//启动通讯服务端口
ss=newDatagramSocket(1111);
}catch(SocketExceptione2){
if(e2.getMessage().startsWith("Addressalreadyinuse")){
showMessageDialog("服务端口被占用,或者本软件已经运行。
");
}
System.exit(0);
}
{//初始化公共信息按钮
messageAlertIcon=newImageIcon(EQ.class.getResource("/image/messageAlert.gif"));
messageAlertNullIcon=newImageIcon(EQ.class.getResource("/image/messageAlertNull20.gif"));
messageStack=newStack();
messageAlertButton=newJButton();
messageAlertButton.setHorizontalAlignment(SwingConstants.RIGHT);
messageAlertButton.setContentAreaFilled(false);//不填充内容区域
finalJPanelBannerPanel=newJPanel();
BannerPanel.setLayout(newBorderLayout());
this.add(BannerPanel,BorderLayout.NORTH);
userInfoButton=newJButton();
BannerPanel.add(userInfoButton,BorderLayout.WEST);
userInfoButton.setMargin(newInsets(0,0,0,10));//Margin:
边距 insets:
插入
initUserInfoButton();//初始化本地用户头像按钮
BannerPanel.add(messageAlertButton,BorderLayout.CENTER);
messageAlertButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEventarg0){
if(!
messageStack.empty()){
showMessageDialog(messageStack.pop());//堆栈顶部的对象(Vector对象中的最后一项)。
}
}
});
messageAlertButton.setIcon(messageAlertIcon);
showMessageBar();
}
this.add(tabbedPane,BorderLayout.CENTER);
tabbedPane.setTabPlacement(SwingConstants.LEFT);//设置此选项卡窗格的选项卡布局
ImageIconuserTicon=newImageIcon(EQ.class.getResource("/image/tabIcon/tabLeft.PNG"));
tabbedPane.addTab(null,userTicon,createUserList(),"用户列表");
ImageIconsysOTicon=newImageIcon(EQ.class.getResource("/image/tabIcon/tabLeft2.PNG"));
tabbedPane.addTab(null,sysOTicon,createSysToolPanel(),"系统操作");
ImageIconsysSTicon=newImageIcon(EQ.class.getResource("/image/tabIcon/tabLeft3.png"));
tabbedPane.addTab(null,sysSTicon,createSysSetPanel(),"系统设置");
this.setAlwaysOnTop(true);//总在顶部
}
//----------------------------------------------------------------------------------
privateJScrollPanecreateSysSetPanel(){//系统设置面板
finalJPanelsysSetPanel=newJPanel();
JScrollPanescrollPane=newJScrollPane(sysSetPanel);
sysSetPanel.setLayout(newBoxLayout(sysSetPanel,BoxLayout.Y_AXIS));
scrollPane.setBorder(newEmptyBorder(0,0,0,0));
finalJPanelsysPathPanel=newJPanel();
sysPathPanel.setMaximumSize(newDimension(600,200));
sysPathPanel.setBorder(newTitledBorder("系统路径"));//title-边框应显示的标题
sysPathPanel.setLayout(newGridLayout(0,1));
sysSetPanel.add(sysPathPanel);
sysPathPanel.add(newJLabel("程序升级路径"));
updatePathTField=newJTextField(preferences.get("updatePath","请输入路径"));
sysPathPanel.add(updatePathTField);
sysPathPanel.add(newJLabel("系统公告路径:
"));
placardPathTField=newJTextField(preferences.get("placardPath","请输入路径"));
sysPathPanel.add(placardPathTField);
sysPathPanel.add(newJLabel("公共程序路径:
"));
pubPathTField=newJTextField(preferences.get("pubPath","请输入路径"));
sysPathPanel.add(pubPathTField);
finalJButtonpathOKButton=newJButton("确定");
pathOKButton.setActionCommand("sysOK");
pathOKButton.addActionListener(newSysSetPanelOKListener());
sysSetPanel.add(pathOKButton);
finalJPanelloginPanel=newJPanel();
loginPanel.setMaximumSize(newDimension(600,90));
loginPanel.setBorder(newTitledBorder("登录升级服务器"));
finalGridLayoutgridLayout_1=newGridLayout(0,1);
gridLayout_1.setVgap(5);
loginPanel.setLayout(gridLayout_1);
sysSetPanel.add(loginPanel);
finalJPanelpanel_7=newJPanel();
panel_7.setLayout(newBoxLayout(panel_7,BoxLayout.X_AXIS));
loginPanel.add(panel_7);
panel_7.add(newJLabel("用户名:
"));
userNameTField=newJTextField(preferences.get("username","请输入用户名"));
panel_7.add(userNameTField);