📄 p2psys.java
字号:
import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.*;
import java.awt.*;
import java.util.Hashtable;
import java.util.Vector;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.Date;
/*ActionListener用于接收操作事件的侦听器接口。
* 对处理操作事件感兴趣的类可以实现此接口,
* 而使用该类创建的对象可使用组件的 addActionListener 方法向该组件注册。
* 在发生操作事件时,调用该对象的 actionPerformed 方法。
* 侦听器的作用,使应用程序主动获取消息,并运行相关方法 */
public class P2pSys extends JFrame implements ActionListener
{
/*Vector 类可以实现可增长的对象数组。
* 与数组一样,它包含可以使用整数索引进行访问的组件。
* 但是,Vector 的大小可以根据需要增大或缩小,以适应创建 Vector 后进行添加或移除项的操作。
*/
//Vector ipItem=new Vector(10,10);//ip对象数组
//Vector messageItem=new Vector(10,10);//消息对象数组
Hashtable ipItem = new Hashtable(10,(float) 0.8);//存放以已连接用户ip,判断用户是否已在列表中
JButton sendButton;//发送按钮
JButton getIpButton;//获得IP按钮
String fileName;
//JButton clearButton;
//JButton downloadButton,nodownloadButton;
//打开文件
JButton fileButton;
JFileChooser fc;
int receivefileover = 0;
//代开历史纪录
JButton historyButton;
ServerSocket serverSkt;
JTextArea messageTextField;//消息输入区
JTextField fileField;//文件输入区
JScrollPane scrollMessage,scrollMessage2,scrollMsg,scrollMessagefrom;//消息滚动
JTextField ipTextField;//IP显示区
DefaultListModel modeIp=new DefaultListModel();//IP列表控制,Swing组件
DefaultListModel modeMsg=new DefaultListModel();//消息列表操作
DefaultListModel modeMsg2=new DefaultListModel();//发送消息 列表操作
DefaultListModel modeMsg3=new DefaultListModel();//收到消息 列表操作
JList ipList;//ip列表对象
JList messageList,tomessageList,frommessageList;//消息列表对象
/*输入输出口提示*/
JLabel messageLabel;
JLabel ipLabel;
JLabel inputLabel;
JLabel inputMessageLabel;
JLabel fileLabel;
JScrollPane scrollIp;
XmlFormat xmlFormat = new XmlFormat();//格式化纯文本数据,将字符串能符合XML使用
XmlParse xmlParse = new XmlParse();
Sender sender = new Sender(P2pSys.this);//声明Sender对象,发送多播消息
ReceiveMutiCast receiveMutiCast = new ReceiveMutiCast(P2pSys.this);//接收多播消息
ReceiveP2p receiveP2p = new ReceiveP2p(P2pSys.this);//接收点到点消息
InfoQueue infoQueue = new InfoQueue();//消息队列
InfoResponse infoResponse = new InfoResponse(P2pSys.this);
FileTransfer fileTransfer = new FileTransfer(P2pSys.this);
FILEHashtable filehashtable = new FILEHashtable();
FileOperation fileoperation = new FileOperation();
IconListItem item ;//头像
SimpleP2pSys simplep2psys;
int SimpleP2pSysShow = 0;
String selectip;
String receivemsg = "";//收到消息
long clickTime = 0;//双击
String myface = "face/14-3.gif";
String myName = getLocalHostname();
ImageIcon selecticon;
//好友上限
int friendnumber = 20;
String myfriend[] = new String[friendnumber];
//myemail 唯一标实
String myemail;
//好友列表
String myfriendemail[] = new String[friendnumber];
//小组
JTree tree;
DefaultTreeModel treemodel;
JScrollPane scrolltree;
//右键菜单
JPopupMenu popupMenu = new JPopupMenu();
JMenu groupmenu = new JMenu("加入小组");
//JMenuItem menuItem3 = new JMenuItem();
int SelectedIndex = -1;
//文件菜单
public JMenu buildFileMenu()
{
//栏目
JMenu fileMenu = new JMenu("文件");
//子栏目
JMenuItem exit=new JMenuItem("退出");
//theMenu.addSeparator();//分隔线
//快捷键
exit.setMnemonic('X');
exit.setAccelerator( KeyStroke.getKeyStroke('X', java.awt.Event.CTRL_MASK, false) );
//方法
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
fileMenu.add(exit);
return fileMenu;
}//end of buildFileMenu()
//风格菜单
public JMenu buildstyleMenu()
{
//栏目
JMenu styleMenu = new JMenu("风格");
//子栏目
JMenuItem stylesimplep2psys=new JMenuItem("简约风格");
//theMenu.addSeparator();//分隔线
//快捷键
stylesimplep2psys.setMnemonic('s');
stylesimplep2psys.setAccelerator( KeyStroke.getKeyStroke('s', java.awt.Event.CTRL_MASK, false) );
//方法
stylesimplep2psys.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
simplep2psys = new SimpleP2pSys(P2pSys.this);
SimpleP2pSysShow = 1;
}
});
styleMenu.add(stylesimplep2psys);
return styleMenu;
}//end of buildFileMenu()
//创建选项卡
JTabbedPane tabbedPane=new JTabbedPane();
//选项卡项目
JPanel messagepane=new JPanel();
JPanel hmessagepane=new JPanel();
//charactersetsys.java 是否为空
boolean fileisnull;
public P2pSys()
{
Thread t=new Thread(new StartLogo());
t.start();
try
{
t.join();
}
catch (InterruptedException ex)
{
}
JFrame.setDefaultLookAndFeelDecorated(true);
//用户接口
//主框架
setTitle("Send");
getContentPane().setLayout(null);
setSize(500,620);
setResizable(false);
setLocation(200,100);
//显示历史
frommessageList= new JList(modeMsg3);
scrollMessagefrom = new JScrollPane(frommessageList);
//----------其他按钮或文本框-----------
getIpButton=new JButton("启动");
getIpButton.setBounds(385 ,220 ,80,30);
Icon getIpButtonicon=new ImageIcon(P2pSys.class.getResource("SystemImage/getIpButtonicon.gif"));
getIpButton.setIcon(getIpButtonicon);
getContentPane().add(getIpButton);
getIpButton.addActionListener(this);
inputLabel=new JLabel("发送对象:");
inputLabel.setBounds(10 ,270 ,170,30);
getContentPane().add(inputLabel);
//编辑消息 或 已发送消息历史
inputMessageLabel=new JLabel("");
inputMessageLabel.setBounds(10 ,330 ,100,30);
getContentPane().add(inputMessageLabel);
inputMessageLabel.setText("编辑消息");
//inputMessageLabel.hide();
ipTextField=new JTextField("");
ipTextField.setBounds(80 ,270 ,275,30);
ipTextField.setForeground(Color.red );
getContentPane().add(ipTextField);
ipTextField.setEnabled(false);
//消息编辑区
messageTextField=new JTextArea("");
messageTextField.setForeground(Color.darkGray);
messageTextField.setEditable(true);
scrollMsg = new JScrollPane(messageTextField);
scrollMsg.setBounds(10 ,360 ,470,140);
getContentPane().add(scrollMsg) ;
//发送按钮
sendButton=new JButton("发送");
sendButton.setBounds(310,510,80,30);
getContentPane().add(sendButton);
sendButton.addActionListener(this);
//显示历史信息 to.....
tomessageList = new JList(modeMsg2);
scrollMessage2 = new JScrollPane(tomessageList);
scrollMessage2.setBounds(10 ,360 ,470,140);
getContentPane().add(scrollMessage2);
scrollMessage2.hide();
//显示历史
historyButton=new JButton("显示记录");
historyButton.setBounds(390,510,90,30);
getContentPane().add(historyButton);
historyButton.addActionListener(this);
historyButton.setEnabled(true);
//
fileLabel=new JLabel("文件路径:");
fileLabel.setBounds(10 ,300 ,80,30);
getContentPane().add(fileLabel);
//
fileField=new JTextField("");
fileField.setBounds(80 ,300 ,275,30);
getContentPane().add(fileField);
fileField.setEnabled(false);
//
fileButton=new JButton("打开");
fileButton.setBounds(385 ,300 ,80,30);
getContentPane().add(fileButton);
fileButton.addActionListener(this);
//Menu菜单
JMenuBar MBar = new JMenuBar();
JMenu filemenu = buildFileMenu();
JMenu stylemenu = buildstyleMenu();
MBar.add(filemenu);
MBar.add(stylemenu);
setJMenuBar(MBar);
//显示msg的列表
messageList= new JList(modeMsg);
scrollMessage = new JScrollPane(messageList);
//选项卡
tabbedPane.setTabPlacement(JTabbedPane.TOP);
tabbedPane.setBounds(10 ,10 ,350,240);
//messagelist的选项卡
messagepane.setLayout( new BorderLayout() );//布局
messagepane.add(scrollMessage);
tabbedPane.addTab("最新消息",messagepane);
getContentPane().add(tabbedPane);
//frommessageList的选项卡
hmessagepane.setLayout( new BorderLayout() );//布局
hmessagepane.add(scrollMessagefrom);
tabbedPane.addTab("全部消息",hmessagepane);
getContentPane().add(tabbedPane);
//头像
try
{
fileisnull = fileIsNull("CharacterSet/CharacterSet.txt","CharacterSet.txt");
}
catch (IOException e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
if(fileisnull)
{
String characterset = "<name>"+myName+"</name>"+"<face>"+myface+"</face>";
fileoperation = new FileOperation();
fileoperation.savePerformed("CharacterSet/CharacterSet.txt",characterset);
}
try
{
String line = fileoperation.openPerformed("CharacterSet/CharacterSet.txt");
String nameline = xmlParse .parse(line,"name").trim() ;
String faceline = xmlParse .parse(line,"face").trim() ;
String emailpassline = fileoperation.openPerformed("EmailPass/EmailPass");
String emailline = xmlParse .parse(emailpassline,"email").trim() ;
if(!nameline.equals("")&&!faceline.equals(""))
{
myName = nameline;
myface = faceline;
}
if(!emailline.equals(""))
{
myemail = emailline;
System.out.println(myemail+" myemail");
}
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
String myfriendline = "";
RandomAccessFile file = new RandomAccessFile("MyFriend/MyFriend","r");
long filepoint=0;
long length=file.length();
int i = 0;
while(filepoint<length)
{
myfriendline = file.readLine();
String myfriendemailline = xmlParse .parse(myfriendline,"email").trim() ;
myfriendemail[i] = myfriendemailline;
System.out.println(myfriendemail[i] + " myfriendemailnnn"+i);
i = i + 1;
}
file.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
//右键菜单
initGroupmenu();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -