JTextArea 动态更新的问题(在程序未控制台结束前就显示,就像控制台那样)

package 测试2;

import java.awt.EventQueue;

public class Fwq {

private JFrame fr;
final JTextArea jt;
private JPanel jp;
private JButton jb;
private JButton gb;

Fwq() {
    fr = new JFrame("服务器");
    fr.setBounds(0, 0, 300, 300);

    jp = new JPanel();
    jp.setBounds(0, 0, 300, 300);
    jp.setLayout(null);

    jb = new JButton("启动服务器");
    jb.setBounds(30, 30, 100, 40);

    gb = new JButton("关闭服务器");
    gb.setBounds(170, 30, 100, 40);

    jt = new JTextArea("");
    jt.setBounds(40, 100, 200, 150);
    jt.setEditable(false);// 不可编辑

    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            Server server = new Server();




            try {

                server.doServer(jt);

                if ("服务器已启动".equals(jt.getText())) {
                    server.GOServer();
                }
            } catch (IOException e) {

                e.printStackTrace();
            }

        }
    });

    gb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

        }
    });

    fr.setVisible(true);
    jp.add(jt);
    jp.add(gb);
    jp.add(jb);
    fr.add(jp);

}

public static void main(String[] args) throws IOException {
    new Fwq();
}

}

package 测试2;

import java.io.*;
import java.net.*;
import java.util.ArrayList;

import javax.swing.JTextArea;

public class Server {
int id = 0;
ArrayList lianjie = new ArrayList();
String xiaoxi = null;
xiancheng xc;
ServerSocket fwq;

void doServer(JTextArea jt) throws IOException {// 启动服务器
    fwq = new ServerSocket(8888);
    System.out.println("已启动");

    jt.append("服务器已启动");
    jt.paintImmediately(jt.getBounds());

}

void GOServer(){//等待连接
    System.out.println("等待连接");
    while (true) {
        Socket socket;
        try {
            socket = fwq.accept();
            id++;
        lianjie.add(socket);
        xc = new xiancheng(socket, id);
        xc.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

void EServer(){//关闭服务器
try {
    fwq.close();
} catch (IOException e) {

    e.printStackTrace();
}
}

class xiancheng extends Thread {

    Socket socket;
    int id;

    public xiancheng(Socket socket, int id) {
        this.socket = socket;
        this.id = id;
    }

    private void fanhui(Socket socket, String xiaoxi) {

        try {
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os);
            pw.println("游客" + xiaoxi);
            pw.flush();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

    public void run() {

        try {
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            while (true) {
                xiaoxi = br.readLine();

                if (xiaoxi.equals("end")) {

                    break;
                }

                System.out.println("客户端信息:" + id + xiaoxi);

                xiaoxi = id + ":" + xiaoxi;

                if (xiaoxi.length() > 0) {

                    for (int i = 0; i < lianjie.size(); i++) {
                        Socket socket = (Socket) lianjie.get(i);

                        fanhui(socket, xiaoxi);

                    }

                } 
                else {

                    Thread.sleep(500);
                }

            }

        } catch (IOException  |InterruptedException e) {
        }

    }

}

// public static void main(String[] args) throws I




OException {
// Server server = new Server();
// server.doServer();
// }

}




Fwq类的按钮jb,点击以后创建Server类对象server,server调用dosrever(jt)函数启动服务器并jt.append("服务器已启动"),jt.paintImmediately(jt.getBounds())。然后判断if ("服务器已启动".equals(jt.getText())) {server.GOServer();}。可运行时,jt.paintImmediately(jt.getBounds())没效果。

1个回答

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