171 lines
5.8 KiB
Java
171 lines
5.8 KiB
Java
|
|
package 大作业;
|
|||
|
|
|
|||
|
|
import javax.swing.JFrame;
|
|||
|
|
import java.awt.GridBagLayout;
|
|||
|
|
import java.awt.Color;
|
|||
|
|
import java.awt.GridBagConstraints;
|
|||
|
|
import java.awt.event.*;
|
|||
|
|
|
|||
|
|
import java.io.IOException;
|
|||
|
|
|
|||
|
|
import java.net.InetAddress;
|
|||
|
|
import java.net.ServerSocket;
|
|||
|
|
import java.net.Socket;
|
|||
|
|
|
|||
|
|
import javax.swing.JButton;
|
|||
|
|
import javax.swing.JTextArea;
|
|||
|
|
import javax.swing.JScrollPane;
|
|||
|
|
|
|||
|
|
public class ChatingRoom extends JFrame implements Runnable{
|
|||
|
|
public JTextArea chat_Frame=new JTextArea();
|
|||
|
|
private JScrollPane pane1=new JScrollPane(chat_Frame);
|
|||
|
|
private JTextArea edit_Text=new JTextArea();
|
|||
|
|
private JScrollPane pane2=new JScrollPane(edit_Text);
|
|||
|
|
private JButton sent_Button=new JButton("发送");
|
|||
|
|
private JButton return_Button=new JButton("退出聊天室");
|
|||
|
|
public String text;
|
|||
|
|
public String Name;
|
|||
|
|
private String Ip;
|
|||
|
|
private int option;
|
|||
|
|
public boolean isClose;
|
|||
|
|
public boolean alsent;
|
|||
|
|
|
|||
|
|
public ChatingRoom(String name,String IP,int option){
|
|||
|
|
Name=name;
|
|||
|
|
Ip=IP;
|
|||
|
|
this.option=option;
|
|||
|
|
isClose=false;
|
|||
|
|
return_Button.setBackground(Color.red);
|
|||
|
|
chat_Frame.setBorder(null);
|
|||
|
|
chat_Frame.setOpaque(false);
|
|||
|
|
pane1.setBorder(null);
|
|||
|
|
pane1.setOpaque(false);
|
|||
|
|
chat_Frame.setEditable(false);
|
|||
|
|
|
|||
|
|
setSize(800, 500);setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);setLocationRelativeTo(null);
|
|||
|
|
setLayout(new GridBagLayout());
|
|||
|
|
GridBagConstraints c=new GridBagConstraints();
|
|||
|
|
c.fill=GridBagConstraints.HORIZONTAL;
|
|||
|
|
c.gridx=0;c.gridy=0;
|
|||
|
|
c.gridwidth=2;
|
|||
|
|
c.ipady=200;
|
|||
|
|
add(pane1, c);
|
|||
|
|
c.gridx=0;c.gridy=1;
|
|||
|
|
c.gridwidth=2;
|
|||
|
|
c.weightx=3;c.weighty=3;
|
|||
|
|
c.ipady=100;
|
|||
|
|
add(pane2, c);
|
|||
|
|
c.gridx=0;c.gridy=2;
|
|||
|
|
c.gridwidth=1;
|
|||
|
|
c.weightx=1;c.weighty=1;
|
|||
|
|
c.ipady=10;
|
|||
|
|
add(sent_Button, c);
|
|||
|
|
c.gridx=1;c.gridy=2;
|
|||
|
|
c.gridwidth=1;
|
|||
|
|
c.weightx=1;c.weighty=1;
|
|||
|
|
c.ipady=10;
|
|||
|
|
add(return_Button, c);
|
|||
|
|
|
|||
|
|
return_Button.addActionListener(new ActionListener() {
|
|||
|
|
public void actionPerformed(ActionEvent e){
|
|||
|
|
Log_In_frame log_In_frame=new Log_In_frame();
|
|||
|
|
log_In_frame.setTitle("登录界面");
|
|||
|
|
log_In_frame.setVisible(true);
|
|||
|
|
isClose=true;
|
|||
|
|
dispose();
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
if(option==2){
|
|||
|
|
sent_Button.addActionListener(new ActionListener() {
|
|||
|
|
public void actionPerformed(ActionEvent e){
|
|||
|
|
chat_Frame.append(text+'\n');
|
|||
|
|
edit_Text.setText("");
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}else if(option==1){
|
|||
|
|
sent_Button.addActionListener(new ActionListener() {
|
|||
|
|
public void actionPerformed(ActionEvent e){
|
|||
|
|
edit_Text.setText("");
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
public void run(){
|
|||
|
|
if (option==2){
|
|||
|
|
setVisible(true);
|
|||
|
|
final int PORT=3030;
|
|||
|
|
ServerSocket s=null;
|
|||
|
|
try{
|
|||
|
|
s=new ServerSocket(PORT);
|
|||
|
|
System.out.println("启动服务器"+'\n'+s);
|
|||
|
|
Socket socket=null;
|
|||
|
|
while (true) {
|
|||
|
|
try {
|
|||
|
|
socket=s.accept();
|
|||
|
|
ServerWorker serverWorker=new ServerWorker(socket,this);
|
|||
|
|
ServerManager.getServetManager().add(serverWorker);
|
|||
|
|
sent_Button.addActionListener(new ActionListener() {
|
|||
|
|
public void actionPerformed(ActionEvent e){
|
|||
|
|
text=Name+": "+edit_Text.getText();
|
|||
|
|
alsent=true;
|
|||
|
|
serverWorker.out.println(text);
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
} catch (IOException e1) {
|
|||
|
|
System.out.println(e1);
|
|||
|
|
System.out.println("获取客户端Scoket异常");
|
|||
|
|
}
|
|||
|
|
return_Button.addActionListener(new ActionListener() {
|
|||
|
|
public void actionPerformed(ActionEvent e){
|
|||
|
|
ServerManager.getServetManager().publishall(Name+"退出聊天室"+'\n'+"聊天室关闭");
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
} catch (IOException e1) {
|
|||
|
|
System.out.println(e1);
|
|||
|
|
System.out.println("ServerSocket创建异常");
|
|||
|
|
} finally{
|
|||
|
|
try {
|
|||
|
|
if(s!=null)
|
|||
|
|
s.close();
|
|||
|
|
} catch (IOException e1) {
|
|||
|
|
System.out.println(e1);
|
|||
|
|
System.out.println("ServerSocket关闭异常");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(option==1){
|
|||
|
|
setVisible(true);
|
|||
|
|
try {
|
|||
|
|
InetAddress addr=InetAddress.getByName(Ip);
|
|||
|
|
CilentSocketMultThread cilentSocketMultThread=new CilentSocketMultThread(addr, this);
|
|||
|
|
sent_Button.addActionListener(new ActionListener() {
|
|||
|
|
public void actionPerformed(ActionEvent e){
|
|||
|
|
text=Name+": "+edit_Text.getText();
|
|||
|
|
alsent=true;
|
|||
|
|
// edit_Text.setText("");
|
|||
|
|
cilentSocketMultThread.out.println(text);
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
return_Button.addActionListener(new ActionListener() {
|
|||
|
|
public void actionPerformed(ActionEvent e){
|
|||
|
|
cilentSocketMultThread.out.println(Name+"退出了聊天室");
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
} catch (IOException e1) {
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|