📄 chatarea.java~4~
字号:
package chat;
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.text.*;
import java.net.*;
public class ChatArea extends JPanel implements Runnable{
BorderLayout borderLayout1 = new BorderLayout();
Action action=new Action();
static Styles style=new Styles();
static Font font=new Font("新宋体",Font.PLAIN,12);
static Color color=Color.black;
static boolean underLine=false;
static boolean strikeThrough=false;
static boolean superscript=false;
static boolean subscript=false;
FileDisposeButton fb;//=new FileDisposeButton("dd","ok","12.",2560);
static LoginDialog loginDialog=new LoginDialog();
final JFileChooser chooser=new JFileChooser();
JFontChooser fontChooser=new JFontChooser(new JFrame());
JButton jButton1 = new JButton();
JMenuBar jmenuBar=new JMenuBar();
JMenu loginMenu=new JMenu();
JMenu fileMenu=new JMenu();
JMenuItem login=new JMenuItem();
JMenuItem loginOut=new JMenuItem();
JMenuItem sendFile=new JMenuItem();
static Hashtable saveFileDisposeButton=new Hashtable();
static JTextPane publicChat=new JTextPane();
static JTextPane privateChat=new JTextPane();
static Document publicChatDocument=publicChat.getDocument();
static Document privateChatDocument=privateChat.getDocument();
static MutableAttributeSet char_style=new SimpleAttributeSet();
static StyledDocument privateDoc=privateChat.getStyledDocument();
StyledDocument publicDoc=publicChat.getStyledDocument();
//String[] items = {"大家"};
DefaultListModel model = new DefaultListModel();
JList peopleList=new JList(model);
JScrollPane comjscrollPane=new JScrollPane(publicChat);
JScrollPane perjscrollPane=new JScrollPane(privateChat);
JScrollPane listPane=new JScrollPane(peopleList);
JSplitPane jsplitPane1=new JSplitPane(JSplitPane.VERTICAL_SPLIT,
comjscrollPane,perjscrollPane);
JSplitPane jsplitPane2=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
listPane,jsplitPane1);
JPanel jpanel1=new JPanel();
JPanel jpanel2=new JPanel();
JPanel jpanel3=new JPanel();
JPanel jpanel4=new JPanel();
JPanel jpanel5=new JPanel();
JTextField messageField=new JTextField(40);
JButton sendMessageButton=new JButton();
//JButton agreeButton=new JButton();
//JButton otherSaveButton=new JButton();
JButton fontButton=new JButton("字体");
/* new ActionListener(){
public void actionPerformed(ActionEvent e){
}
})*/
JButton clearSendObjectButton=new JButton();
static JLabel sendObject=new JLabel(" ");
File file=null;
static Hashtable peopleTable=new Hashtable();
static DataOutputStream out=null;
DataInputStream in=null;
static Socket socket=null;
Thread messageThread=null;
Vector toPeople=new Vector();
String selectName="";
StringBuffer selectNameSB=new StringBuffer();
String userName;
public ChatArea(Hashtable peopleTable) {
this.peopleTable=peopleTable;
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
listPane.setPreferredSize(new Dimension(70,75));
centerOnScreen(loginDialog);
centerOnScreen(fontChooser);
loginMenu.setEnabled(false);
fileMenu.setEnabled(false);
sendMessageButton.setEnabled(false);
clearSendObjectButton.setEnabled(false);
// peopleList.setModel(model);
publicChat.setEditable(false);
privateChat.setEditable(false);
jButton1.setText("jButton1");
loginDialog.setVisible(true);
loginMenu.setText("登陆");
fileMenu.setText("文件");
login.setText("登陆");
loginOut.setText("退出");
sendFile.setText("发送文件");
jmenuBar.add(loginMenu);
jmenuBar.add(fileMenu);
loginMenu.add(login);
loginMenu.add(loginOut);
fileMenu.add(sendFile);
jsplitPane1.setDividerSize(12);
jsplitPane1.setOneTouchExpandable(true);
jsplitPane2.setDividerSize(12);
jsplitPane2.setOneTouchExpandable(true);
sendMessageButton.setText("发送");
//agreeButton.setText("保存");
//disagreeButton.setText("拒绝");
//otherSaveButton.setText("另存为");
clearSendObjectButton.setText("清除选定对像");
privateChat.setBackground(new Color(140,173,215));
publicChat.setBackground(new Color(140,173,215));
jpanel1.setLayout(new GridLayout(1,2));
jpanel2.setLayout(new FlowLayout());
//jpanel3.setLayout(new GridLayout(1,2));
//jpanel4.setLayout(new GridLayout(1,3));
jpanel5.setLayout(new GridLayout(2,1));
jpanel1.add(jpanel2);
jpanel1.add(jpanel5);
jpanel2.add(messageField);
jpanel3.setLayout(new BorderLayout());
jpanel3.add(sendMessageButton,BorderLayout.WEST);
jpanel3.add(sendObject,BorderLayout.CENTER);
jpanel3.add(clearSendObjectButton,BorderLayout.EAST);
jpanel4.add(fontButton);
//jpanel4.add(disagreeButton);
//jpanel4.add(otherSaveButton);
jpanel5.add(jpanel3);
jpanel5.add(jpanel4);
// jsplitPane.setLocation(20,20);
this.setLayout(borderLayout1);
this.add(jmenuBar, BorderLayout.NORTH);
this.add(jsplitPane2,BorderLayout.CENTER);
this.add(jpanel1,BorderLayout.SOUTH);
//this.add(listPane,BorderLayout.EAST);
fontButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int state=fontChooser.showDialog(font,color,underLine,strikeThrough,superscript,subscript);
//System.out.println(font.getName()+" "+font.getSize());
if(state==JFontChooser.OK_OPTION){
font = fontChooser.getFont();
color=fontChooser.getColor();
underLine=fontChooser.getUndeline();
strikeThrough=fontChooser.getStrikeThrough();
superscript=fontChooser.getSuperscipt();
subscript=fontChooser.getSubscripte();
//messageField.setFont(font);
//messageField.setForeground(color);
//System.out.println(font.getName()+" "+font.getSize());
}
}
});
messageField.addActionListener(action);
login.addActionListener(action);
loginOut.addActionListener(action);
sendFile.addActionListener(action);
sendMessageButton.addActionListener(action);
clearSendObjectButton.addActionListener(action);
peopleList.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
JList list = (JList)me.getSource();
if(me.getClickCount()==1){
selectName= (String) peopleList.getSelectedValue();
if(!toPeople.contains(selectName)){
toPeople.addElement(selectName);
selectNameSB.append(selectName+" ");
}
//Enumeration enum=toPeople.elements();
ChatArea.sendObject.setText(selectNameSB.toString());
}
}
});
messageThread=new Thread(this);
peopleList.setToolTipText("单击,选择聊天或者发送文件的对象");
sendObject.setToolTipText("选定的聊天对象");
clearSendObjectButton.setToolTipText("清空选定的对象");
}
public void setConnection(Socket socket,DataOutputStream out,DataInputStream in){
this.socket=socket;
this.out=out;
this.in=in;
loginMenu.setEnabled(true);
fileMenu.setEnabled(true);
sendMessageButton.setEnabled(true);
clearSendObjectButton.setEnabled(true);
userName=loginDialog.getName();
/*try{
this.in = new DataInputStream(this.socket.getInputStream());
}catch(Exception e){
}*/
try{
//System.out.println(socket.getLocalPort()+"chatarea");
messageThread.start();
}catch(Exception e){
e.printStackTrace();
}
}
public static Socket getSocket(){ return socket; }
public static DataOutputStream getOut(){ return out;}
public void run(){
//byte[] buffer=new byte[256];
while (true) {
String _message = null;
try {
_message = in.readUTF();
if (_message.startsWith("PRIVATE_MESSAGE:")) {
String headMessage=_message.substring(_message.indexOf("PRIVATEMESSAGE:")+17,_message.indexOf("#CONTENT#"));
String content = _message.substring(_message.indexOf(
"#CONTENT#")+9,_message.lastIndexOf("#FONTATTRIBUTE#"));
String fontMessage=_message.substring(_message.lastIndexOf("#FONTATTRIBUTE#")+15);
String fontFamily=fontMessage.substring(0,fontMessage.indexOf("$"));
int fontStyle=Integer.parseInt(fontMessage.substring(fontMessage.indexOf("$")+1,fontMessage.lastIndexOf("$")));
int fontSize=Integer.parseInt(fontMessage.substring(fontMessage.lastIndexOf("$")+1));
Font font=new Font(fontFamily,fontStyle,fontSize);
//System.out.println(fontFamily+" "+fontStyle+" "+fontSize);
try {
// privateChatDocument.insertString(privateChatDocument.getLength(),
// content + "\n", char_style);
style.addStylesToDocument(privateDoc,font);
privateDoc.insertString(privateDoc.getLength(), headMessage,
privateDoc.getStyle("regular"));
privateDoc.insertString(privateDoc.getLength(), content + "\n",
privateDoc.getStyle("newStyle"));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -