?? chatarea.java~395~
字號:
try {
// privateChatDocument.insertString(privateChatDocument.getLength(),
// content + "\n", char_style);
style.addStylesToDocument(privateDoc);
privateDoc.insertString(privateDoc.getLength(), content + "\n",
privateDoc.getStyle("regular"));
}
catch (BadLocationException e) {
e.printStackTrace();
}
}
else if (_message.startsWith("PUBLIC_MESSAGE:")) {
String content = _message.substring(_message.indexOf(
"PUBLIC_MESSAGE:") + 15);
try {
// publicChatDocument.insertString(publicChatDocument.getLength(),
// content + "\n", char_style);
style.addStylesToDocument(publicDoc);
publicDoc.insertString(publicDoc.getLength(), content + "\n",
publicDoc.getStyle("regular"));
}
catch (BadLocationException e) {
e.printStackTrace();
}
}
else if (_message.startsWith("LOGIN_PEOPLE:")) {
String peopleName = _message.substring(_message.indexOf(":") + 1);
peopleTable.put(peopleName, peopleName);
model.addElement( (String) peopleTable.get(peopleName));
//peopleList.repaint();
peopleList.validate();
}
else if (_message.startsWith("SEND_FILE:")) {
// System.out.println(_message.substring(_message.indexOf("SEND_FILE:")+10));
String _requestPeople = _message.substring(_message.indexOf(
"SEND_FILE:") + 10, _message.indexOf("#TOSENDFILE#"));
String _toSendFile = _message.substring(_message.indexOf(
"#TOSENDFILE#") + 12, _message.indexOf("#IPADDRESS#"));
String _ipAddress = _message.substring(_message.indexOf("#IPADDRESS#") +
11, _message.indexOf("#PORT#"));
int port = (Integer.valueOf(_message.substring(_message.lastIndexOf(
"#PORT#") + 6, _message.lastIndexOf("#FILE_LENGTH#")))).intValue();
//int port=3620;
String fileLength = _message.substring(_message.lastIndexOf(
"#FILE_LENGTH#") + 13);
//System.out.println(_requestPeople+" "+_toSendFile);
System.out.println(fileLength);
File file = new File(_toSendFile);
try {
fb = new FileDisposeButton(_requestPeople, _toSendFile, _ipAddress,
port, fileLength);
this.saveFileDisposeButton.put(_requestPeople + _toSendFile, fb);
style.addStylesToDocument(privateDoc, fb);
privateDoc.insertString(privateDoc.getLength(), " " + "\n",
privateDoc.getStyle("jpanel"));
privateChat.validate();
}
catch (BadLocationException e) {
e.printStackTrace();
}
}
else if (_message.startsWith("CANCEL_RECEIVED:")) {
//System.out.println(_message);
String _toPeople = _message.substring(_message.indexOf(
"CANCEL_RECEIVED:") + 16, _message.indexOf("#FILENAME#"));
String _fileName = _message.substring(_message.indexOf("#FILENAME#") +
10);
System.out.println(_toPeople + _fileName);
String key = _toPeople + _fileName;
File file = new File(_fileName);
FileCancelButton fileCancelButton = (FileCancelButton)
FileCancelButton.saveFileCancel.get(key);
fileCancelButton.fileInfo.setText(_toPeople + "取消文件[" + file.getName() +
"]的發(fā)送");
fileCancelButton.cancelSendButton.setVisible(false);
FileCancelButton.saveFileCancel.remove(fileCancelButton);
}
else if (_message.startsWith("CANCEL_SENDING:")) {
//System.out.println(_message);
String _toPeople = _message.substring(_message.indexOf(
"CANCEL_SENDING:") + 15, _message.indexOf("#FILENAME#"));
String _fileName = _message.substring(_message.indexOf("#FILENAME#") +
10);
System.out.println(_toPeople + _fileName);
String key = _toPeople + _fileName;
File file = new File(_fileName);
FileDisposeButton fileDisposeButton = (FileDisposeButton)
FileDisposeButton.saveFileDisposeButton.get(key);
fileDisposeButton.fileInfo.setText(_toPeople + "取消文件[" + file.getName() +
"]的發(fā)送");
fileDisposeButton.buttonJPanel.setVisible(false);
FileDisposeButton.saveFileDisposeButton.remove(fileDisposeButton);
}
else if (_message.startsWith("PEOPLE_LEAVE:")) {
System.out.println(_message);
String _peopleName = _message.substring(_message.indexOf(
"PEOPLE_LEAVE:") + 13);
model.removeElement(_peopleName);
peopleList.validate();
peopleTable.remove(_peopleName);
}
Thread.sleep(5);
//}
}
catch (Exception e) {
}
}
}
public void stop(){
try{
this.socket.close();
this.out.flush();
this.out.close();
this.in.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
public static void main(String[] args){
ChatArea chatarea=new ChatArea(new Hashtable());
JFrame f=new JFrame();
f.getContentPane().add(chatarea);
// chatarea.setVisible(true);
//f.setSize(800,700);
f.setVisible(true);
f.pack();
}
class Action implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==sendMessageButton||e.getSource()==messageField){
String peoples="";
try{
String content=messageField.getText().trim();
if(content.length()>0&&selectNameSB.length()>0){
messageField.setText("");
messageField.setFocusable(true);
if (toPeople.contains("大家")) {
publicChatDocument.insertString(publicChatDocument.getLength(),
"您對大家說:" +content + "\n", char_style);
out.writeUTF("PUBLIC_MESSAGE:" + "#MESSAGECONTENT#" + content);
}
else {
Enumeration enum = toPeople.elements();
while (enum.hasMoreElements()) {
peoples = peoples + enum.nextElement() + "$";
}
privateChatDocument.insertString(privateChatDocument.getLength(),
"您對" + selectNameSB + "說:" +
content + "\n", char_style);
out.writeUTF("PRIVATE_MESSAGE:" + "#MESSAGECONTENT#" + content +
"#TOPEOPLE#" + peoples);
}
}else{
sendObject.setText("請選擇對象");
}
}catch(BadLocationException bade){}
catch(IOException e2){
e2.printStackTrace();
}
}
else if(e.getSource()==clearSendObjectButton){
toPeople.removeAllElements();
selectNameSB.delete(0,selectNameSB.length());
sendObject.setText("");
}
else if(e.getSource()==sendFile){
int state=chooser.showOpenDialog(null);
file=chooser.getSelectedFile();
if(file!=null&&state==(chooser.APPROVE_OPTION)){
if (toPeople.contains("大家")) {
sendObject.setText("發(fā)送對象不允許為大家");
sendObject.setForeground(Color.red);
}else{
ClientSendFile clientSendFile = new ClientSendFile(file, toPeople);
}
}
}
else if(e.getSource()==login){
loginDialog.setVisible(true);
}
else if(e.getSource()==loginOut){
if(socket!=null){
try {
socket.close();
out.flush();
out.close();
in.close();
}
catch (IOException ioe) {}
}
System.exit(0);
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -