📄 chatwindow.java
字号:
package client.display;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import client.Client;
import common.message.ChatMessage;
import common.message.Message;
/**
* 与某个用户聊天界面
* @author 彭文杰
* 日期:Oct 21, 2006
*/
public class ChatWindow extends JFrame {
private static final long serialVersionUID = 8574239376259350958L;
//当前用户id
private String userid;
//发送到的用户id
private String touser;
//客户端交互部分
private Client client;
//输入域
private JTextField input;
//显示域
private JTextArea displayArea;
public ChatWindow(String userid,String touser,Client client){
super("与"+touser+"聊天");
this.userid=userid;
this.touser=touser;
this.client=client;
addWindowListener(
new WindowAdapter(){
//定义窗口关闭时,执行移出行为
public void windowClosing(WindowEvent e){
ChatWindow chw=(ChatWindow)e.getSource();
Client client=chw.getClient();
client.unregisterChatWindow(chw);
dispose();
}
}
);
Container container = getContentPane();
this.input=new JTextField();
this.input.setEnabled(true);
this.input.addActionListener(new ChatWindowListener(this){
public void actionPerformed(ActionEvent event) {
ChatWindow chw=this.getChw();
ChatMessage cm=new ChatMessage(chw.input.getText());
cm.setToUser(chw.touser);
cm.setFromUser(chw.userid);
cm.setSendTime(new Date());
chw.client.sendMessage(cm);
chw.displayMessage(cm);
chw.input.setCaretPosition(chw.input.getText().length());
}
});
container.add(this.input,BorderLayout.SOUTH);
this.displayArea=new JTextArea();
container.add(new JScrollPane(this.displayArea),BorderLayout.CENTER);
setSize( 300, 650 );
setVisible( true );
}
/**
* 显示消息
* @author 彭文杰
* 日期:Oct 21, 2006
* @param msg
*/
public void displayMessage(Message msg){
displayArea.append( "\n" + msg.toString());
}
/**
* 关闭窗口
* @author 彭文杰
* 日期:Oct 21, 2006
*/
public void closeWindow(){
dispose();
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -