📄 chatarea.java~390~
字号:
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();
FileDisposeButton fb;//=new FileDisposeButton("dd","ok","12.",2560);
static LoginDialog loginDialog=new LoginDialog();
JFileChooser chooser=new JFileChooser();
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 disagreeButton=new JButton();
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();
public ChatArea(Hashtable peopleTable) {
this.peopleTable=peopleTable;
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
listPane.setPreferredSize(new Dimension(70,75));
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(fb);
//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);
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);
/*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 content = _message.substring(_message.indexOf(
"PRIVATEMESSAGE:") + 17);
try {
// privateChatDocument.insertString(privateChatDocument.getLength(),
// content + "\n", char_style);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -