能不能在jp3板块帮忙添加按钮插入图片功能和改变一下面板的背景颜色,拜托谢谢各位大神!
public class Client_Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String ipAdd = "127.0.0.1";
        int  port =8000;
        Client  client = new Client(ipAdd,port);
    }

}
import java.awt.BorderLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.*;

public class Client extends JFrame {
    // 成员变量
    JPanel jp1 = new JPanel();
    JPanel jp2 = new JPanel();
    JPanel jp3 = new JPanel();
    JTextArea jta = new JTextArea(15, 40);
    JTextField jtf = new JTextField();
    JButton jbt1 = new JButton("清除记录");
    JButton jbt2 = new JButton("存储记录");
    JButton jbt3 = new JButton("读取记录");
    JLabel jlb = new JLabel();
    FileWriter fw;
    FileReader fr;
    Socket socket = null;
    DataOutputStream dos = null;
    DataInputStream dis = null;
    String ipAdd = "";
    int port = 0;
    String readStr = "";
    String jtaStr = "";

    // 构造方法
    public Client(String ipAdd, int port) {
        setLocation(300, 400);
        setSize(400, 500);
        setVisible(true);
        add(jp1, BorderLayout.NORTH);
        add(jp2, BorderLayout.CENTER);
        add(jp3, BorderLayout.SOUTH);
        jp1.setLayout(new BorderLayout());
        jp1.add(jta, BorderLayout.NORTH);
        jp1.add(jtf, BorderLayout.SOUTH);
        jp2.add(jbt1);
        jp2.add(jbt2);
        jp2.add(jbt3);
        jp3.add(jlb);
        jlb.setText("当前状态:");
        pack();
        this.ipAdd = ipAdd;
        this.port = port;

        // 调用网络连接
        netConnect();

        // 挂载侦听器
        jtf.addActionListener(new MyActionListener());
        jbt1.addActionListener(new MyActionListener());
        jbt2.addActionListener(new MyActionListener());
        jbt3.addActionListener(new MyActionListener());

    }

    // 网络连接方法
    public void netConnect() {
        try {
            socket = new Socket(this.ipAdd, this.port);
            jlb.setText("服务器连接成功" + socket);
            dos = new DataOutputStream(socket.getOutputStream());
            dis = new DataInputStream(socket.getInputStream());

            JtaReadThread jrt = new JtaReadThread();
            Thread thread = new Thread(jrt);
            thread.start();

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 新建侦听器类(内部类)
    private class MyActionListener implements ActionListener {

        public void actionPerformed(ActionEvent arg0) {
            if (arg0.getSource().equals(jtf)) {
                try {
                    dos.writeUTF(jtf.getText());
                    jtf.setText("");
                    // jta.setText(jta.getText()+"\n"+dis.readUTF());

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (arg0.getSource().equals(jbt1)) {
                jta.setText("");
            }
            // 保存记录侦听器
            if (arg0.getSource().equals(jbt2)) {

                byte b[] = jta.getText().getBytes();
                try {
                    fw = new FileWriter("d:\\chatLog2.txt", true);
                    fw.write(jta.getText());
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                jlb.setText("记录保存成功");
            }

            // // 读取记录侦听器------jbt3 :FR读取记录
            if (arg0.getSource().equals(jbt3)) {
                int b = 0;
                try {
                    fr = new FileReader("d:\\chatLog2.txt");
                    while ((b = fr.read()) != -1) {
                        System.out.print((char) b);
                    }
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    // 新建子线程类,用于读取服务器发送的广播数据
    private class JtaReadThread implements Runnable {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    readStr = dis.readUTF();
                    jtaStr = jtaStr + '\n' + readStr;
                    jta.setText(jtaStr );
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }
}

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

//服务器工作过程:
//1.指定某个端口提供TCP服务
//2.等待客户端的连接(阻塞:傻等)
//3.如果需要接收多个客户端,建议使用“死”循环语句
public class Server {
    ServerSocket ss = null;
    Socket socket = null;
    int port =0;
    //集合:用于存储每个客户端和服务器的连接(存储子线程)
    List<SubThread> mylist = new ArrayList<SubThread>();

    Server(int port) {
        this.port = port;
        try {
            ss = new ServerSocket(this.port);
            System.out.println("服务器已经启动,port8000");
            while (true) {
                socket = ss.accept();
                System.out.println("客户端已连接");   
                //传递socket 给子线程,socket参数在构造方法中传递
                SubThread subThread = new SubThread(socket);
                //将构造好的子线程实例存入集合中
                mylist.add(subThread);
                Thread thread = new Thread(subThread);
                thread.start();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //新建内部类:子线程
    private class SubThread implements Runnable{
        //成员变量:通道、读写
        Socket socket = null;
        DataInputStream dis = null;
        DataOutputStream dos = null;
        String strRead = "";
        SubThread(Socket socket){
            this.socket = socket;
            try {
                dis = new DataInputStream(socket.getInputStream());
                dos = new DataOutputStream(socket.getOutputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public void run() {
            // TODO Auto-generated method stub
            //开始读写操作
            while(true){
                try {
                    strRead = dis.readUTF();
                    System.out.println(strRead);

                    for(int i=0; i<mylist.size(); i++){//遍历所有客户端,写!
                        //取得当前的客户端的连接(socket)将strRead内容写给当前的客户端
                        mylist.get(i).dos.writeUTF(strRead);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }





}


public class ServerRun {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int   serverPort = 8000;
        Server server = new Server(serverPort);
    }

}
![图片说明](https://img-ask.csdn.net/upload/201906/27/1561601303_757012.jpg)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问