📄 chatclient.java
字号:
/*
* @author: 先风剑客
* @version: chat0.8
* 实现各按钮事件响应
* 主要是对不同按钮作出反应
* 实现上一版本未完成的功能
* 在对发送后,显示本机发送的内容
* 实现了网络连接
* 对网络上接收的数据进行简单处理
* 在客户端加入了一张图片
* 对发送的处理进一步优化
* 对发送按钮与回车进行事件集成
* 添加时间功能
* 界面显示的屏幕正中央规范化
* 各类异常的人性化处理
*
* 可接收来自其它主机的信息,即可从主机接收
* 实现启动界面雏形
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class ChatClient {
public static void main(String[] args) {
//new Ready();
new ChatClientMonitor("127.0.0.1", 5678);
}
}
class Ready extends Frame{
String ip = null;
int port = 0;
TextField tf1 = null;
TextField tf2 = null;
boolean donext = false;
ChatClientMonitor c = null;
Ready(){
super("设置连接相关参数");
ReadyActionListener ra = new ReadyActionListener();
Label l1 = new Label("连接主机的IP:");
l1.setBackground(Color.red);
tf1 = new TextField(22);
Label l2 = new Label(" 连接端口号: ");
l2.setBackground(Color.red);
tf2 = new TextField(10);
tf2.addActionListener(ra);
Button b1 = new Button("连接");
b1.setBackground(Color.green);
b1.setActionCommand("connect");
b1.addActionListener(ra);
Button b2 = new Button("退出");
b2.setBackground(Color.magenta);
b2.setActionCommand("exit");
b2.addActionListener(ra);
Panel p1 = new Panel();
p1.setBackground(Color.black);
Panel p2 = new Panel();
p2.setBackground(Color.black);
add(p1,BorderLayout.NORTH);
p1.add(l1,BorderLayout.WEST);
p1.add(tf1,BorderLayout.EAST);
add(p2,BorderLayout.SOUTH);
p2.add(l2,BorderLayout.NORTH);
p2.add(tf2,BorderLayout.WEST);
p2.add(b1,BorderLayout.CENTER);
p2.add(b2,BorderLayout.EAST);
setBackground(Color.red);
pack();
Dimension screen = this.getToolkit().getScreenSize();
setLocation((screen.width - getSize().width) / 2, (screen.height - getSize().height) / 2);
setResizable(false);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
});
}
class ReadyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "exit"){
setVisible(false);
System.exit(0);
}else{
setVisible(false);
c = new ChatClientMonitor((tf1.getText()).trim(), Integer.parseInt(tf2.getText().trim()));
//System.exit(0);
}
}
}
}
class ChatClientMonitor extends Frame{
DataOutputStream dos = null;
DataInputStream dis = null;
Socket s = null;
TextArea ta = null;
TextField tf = null;
Date dt = null;
boolean bconnect = false;
String ip = null;
int port = 0;
Thread t = new Thread(new GetData());
ChatClientMonitor(String str, int port){
super("ChatClient");
this.ip = str;
this.port = port;
ChatClientActionListener ca = new ChatClientActionListener();
tf = new TextField(70);
tf.addActionListener(ca);
ta = new TextArea("信息记录:", 12, 60);
URL url = getClass().getResource("/image/feifei.png");//加入图片
Dimension screen = getToolkit().getScreenSize();//显示位置优化
Button b1 = new Button("发送");
b1.setActionCommand("send");
b1.setBackground(Color.green);
b1.addActionListener(ca);
Button b2 = new Button("清空");
b2.setActionCommand("clear");
b2.setBackground(Color.magenta);
b2.addActionListener(ca);
Button b3 = new Button("退出");
b3.setActionCommand("exit");
b3.setBackground(Color.red);
b3.addActionListener(ca);
Panel p = new Panel();
p.setBackground(Color.black);
Panel p1 = new Panel();
p1.setBackground(Color.BLACK);
add(p1,BorderLayout.NORTH);
p1.add(ta,BorderLayout.WEST);
if(url!=null){
p1.add(new JButton(new ImageIcon(url)),BorderLayout.EAST);
}
p.add(tf,BorderLayout.NORTH);
p.add(b1,BorderLayout.WEST);
p.add(b2,BorderLayout.CENTER);
p.add(b3,BorderLayout.EAST);
add(p,BorderLayout.SOUTH);
pack();
setLocation((screen.width - getSize().width) / 2,(screen.height - getSize().height) / 2);
setBackground(Color.red);
setResizable(false);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
try{
if(dos != null)
dos.close();
if(s != null)
s.close();
}catch(IOException e1){
ta.setText(ta.getText()+"\n"+"已经退出连接....");
System.exit(-1);
}
setVisible(false);
System.exit(0);
}
});
connect();//调用连接服务器方法,连接上服务器
t.start();//调用读取数据,线程启动.....
}
private void connect(){
try{
s = new Socket(ip, port);
dt = new Date();
ta.setText("已经连接上服务器:"+s.getRemoteSocketAddress()+" "+dt.getMonth()+"月 "+dt.getDay()+"日 "+dt.getHours()+"时 "+dt.getMinutes()+"分 "+dt.getSeconds()+"秒");
bconnect = true;
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
}catch(IOException e2){
bconnect = false;
ta.setText(ta.getText()+"\n"+"无法找到指定服务器......");
}
}
class GetData implements Runnable{//启用多线程
public void run() {
while(bconnect){
try {
ta.setText(ta.getText()+"\n"+dis.readUTF().trim());
} catch (IOException e) {
bconnect = false;
ta.setText(ta.getText()+"\n"+"与服务器断开连接......");
}
}
}
}
private class ChatClientActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "clear"){
tf.setText("");
}else if(e.getActionCommand() == "exit"){
try{
if(dos != null)
dos.close();
if(s != null)
s.close();
}catch(IOException e1){
System.out.println(e1.getMessage());
System.exit(-1);
}
setVisible(false);
System.exit(0);
}else{
dt = new Date();
//ta.setText((ta.getText()+"\n"+s.getLocalAddress()+": "+dt.getMonth()+"月 "+dt.getDay()+"日 "+dt.getHours()+"时 "+dt.getMinutes()+"分 "+dt.getSeconds()+"秒"+"\n"+" "+tf.getText()).trim());
try{
dos.writeUTF(s.getLocalAddress()+": "+dt.getMonth()+"月 "+dt.getDay()+"日 "+dt.getHours()+"时 "+dt.getMinutes()+"分 "+dt.getSeconds()+"秒"+"\n"+tf.getText());
dos.flush();
}catch(IOException e1){
ta.setText(ta.getText()+"\n"+"发送失败...."+"\n"+"服务器断开连接......");
}
tf.setText("");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -