📄 chatarea.java~1~
字号:
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();
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 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();
String[] items = {"A", "B", "C", "D"};
JList peopleList=new JList(items);
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,jsplitPane1,peopleList);
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();
static JLabel sendObject=new JLabel(" ");
File file=null;
static Hashtable peopleTable=null;
DataOutputStream out;
DataInputStream in;
Socket socket=null;
Thread messageThread=null;
public ChatArea() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
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("另存为");
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.add(sendMessageButton);
jpanel3.add(sendObject);
jpanel4.add(agreeButton);
jpanel4.add(disagreeButton);
jpanel4.add(otherSaveButton);
jpanel5.add(jpanel3);
jpanel5.add(jpanel4);
// jsplitPane.setLocation(20,20);
this.setLayout(borderLayout1);
peopleList.addMouseListener(new MouseAdapter(){
public void mouseClick(MouseEvent me){
JList list = (JList)me.getSource();
if(me.getClickCount()==2){
System.out.println("ok");
String s = (String) peopleList.getSelectedValue();
ChatArea.sendObject.setText(s);
}
}
});
//peopleList.setVisible(true);
//peopleList.setSize(5,5);
//listPane.setSize(10,10);
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);
agreeButton.addActionListener(action);
disagreeButton.addActionListener(action);
otherSaveButton.addActionListener(action);
messageThread=new Thread(this);
}
public void setConnection(Socket socket,DataOutputStream out,DataInputStream in){
this.socket=socket;
this.out=out;
this.in=in;
try{
messageThread.start();
}catch(Exception e){
e.printStackTrace();
}
}
public void run(){
String message=null;
while(true){
try{
message=in.readUTF();
if(message.startsWith("PRIVATEMESSAGE")){
try{
privateChatDocument.insertString(privateChatDocument.getLength(),
message,char_style);
}catch(BadLocationException e){
e.printStackTrace();
}
}
else if(message.startsWith("PUBLICMESSAGE:")){
}
else if(message.startsWith("LOGIN_PEOPLE")){
}
else if(message.startsWith("SENGD_FILE:")){
}
else if(message.startsWith("AGREE_ACCEPT:")){
}
else if(message.startsWith("DISAGREE_ACCEPT:")){
}
else if(message.startsWith("PEOPLE_LEAVE:")){
}
}catch(IOException e){
e.printStackTrace();
}
}
}
public static void main(String[] args){
ChatArea chatarea=new ChatArea();
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()==login){
loginDialog.setVisible(true);
}
else if(e.getSource()==loginOut){
System.exit(0);
}
else if(e.getSource()==sendFile){
int state=chooser.showOpenDialog(null);
file=chooser.getSelectedFile();
if(file!=null&&state==(chooser.APPROVE_OPTION)){
System.out.println(file.getAbsoluteFile()+file.getPath());
}
}
else if(e.getSource()==sendMessageButton){
}
else if(e.getSource()==agreeButton){
}
else if(e.getSource()==disagreeButton){
}
else if(e.getSource()==otherSaveButton){
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -