📄 client.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Client extends Frame{
Socket socket = null;
DataOutputStream dos = null;
DataInputStream dis = null;
//IP PORT 用户姓名
int portNumber;
String ipAddress;
String clientName;
//是否连接
public boolean con = false;
//图形界面元素
JTextField ip = new JTextField();
JTextField name = new JTextField();
JTextField port = new JTextField();
TextField input = new TextField();
TextArea output = new TextArea();
JButton connect = new JButton("CONNECT");
JButton disconnect = new JButton("DISCONNECT");
//用户线程
Thread thread = new Thread(new RecvThread());
public static void main(String[] args) {
Client c = new Client();
c.jbInit();
}
/*
* 设置聊天室界面的大小,位置等
*/
public void jbInit() {
Panel p1 = new Panel();
p1.setSize(100, 200);
Panel p2 = new Panel();
p2.setSize(100, 200);
setLocation(400, 300);
this.setSize(300, 300);
p1.setLayout(new GridLayout(4,2,5,5));
p1.add(new JLabel(" IP:"));
p1.add(ip);
p1.add(new JLabel(" Port:"));
p1.add(port);
p1.add(new JLabel(" Name:"));
p1.add(name);
p1.add(connect);
p1.add(disconnect);
p2.setLayout(new BorderLayout());
p2.add(output, BorderLayout.CENTER);
p2.add(input, BorderLayout.SOUTH);
this.setLayout(new GridLayout(1,1,5,5));
this.add(p1);
this.add(p2);
/*
* void java.awt.Window.pack()
* Causes this Window to be sized to fit the preferred
* size and layouts of its subcomponents. If the window
* and/or its owner are not yet displayable, both are made
* displayable before calculating the preferred size. The
* Window will be validated after the preferredSize is calculated.
*/
pack();
/*
* void java.awt.Window.addWindowListener(WindowListener l)
* Adds the specified window listener to receive window
* events from this window. If l is null, no exception
* is thrown and no action is performed.
*/
this.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}
}
);
//为输入框添加监听器
input.addActionListener((ActionListener) new InputListener());
//连接按钮 建立连接
connect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(e.getSource() == connect ){
connect();
}
}
});
//断开按钮 断开连接
disconnect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(e.getSource() == disconnect)
disconnect();
}
});
//设置框架可见
setVisible(true);
}
/*
* 建立连接
*/
public void connect() {
try {
//接收输入的ip,port号,还有用户名字
ipAddress = ip.getText().trim();
// System.out.println(ipAddress);//调程序用的
portNumber = Integer.parseInt(port.getText().trim());
// System.out.println(portNumber);
clientName = name.getText().trim();
// System.out.println(clientName);
socket = new Socket(ipAddress, portNumber);
// s = new Socket("127.0.0.1", 8888); //调程序用的
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
System.out.println("connected!");
con = true;
// System.out.println(connected);//调程序用的
//启动用户线程
thread.start();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 断开连接
*/
public void disconnect() {
try {
dos.close();
dis.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 接收输入
*/
public class InputListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = input.getText().trim();
input.setText("");
try {
//System.out.println(s); //调程序用的
dos.writeUTF(clientName+" : "+str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
/*
* 输出到聊天室对话框中
* 当断开连接时给出离开聊天室提示
*/
public class RecvThread implements Runnable {
public void run() {
//System.out.println("helloworld");//调程序用的
try {
//System.out.println(connected);//调程序用的
while(con) {
String str = dis.readUTF();
System.out.println(str);
output.setText(output.getText() + str + '\n');
output.setBackground(Color.GRAY);
output.setForeground(Color.GREEN);
}
} catch (SocketException e) {
System.out.println(clientName + "离开!");
} catch (EOFException e) {
System.out.println(clientName + "离开!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -