Java套接字服务器显示空白窗口

时间:2014-05-08 11:24:12

标签: java swing sockets event-dispatch-thread

我正在编写一个通过服务器共享两个客户端的程序当我在没有启动方法的情况下运行它时,它工作正常,看起来像这样

但是当我试图听取连接时,我只得到空白的白色窗口。该计划在conn = server.accept()处停止,这是非常符合逻辑的,因为我还没有客户,但为什么不是这样呢?


另外,我可以以某种方式摆脱那个白色边框吗?

import java.awt.*;
import java.awt.print.PrinterException;
import java.io.*;
import java.net.*;

import javax.swing.*;

public class Server extends JFrame {
private ServerSocket server;
private Socket conn;
private JTextArea TA;
private JPanel panel1;
private final int port = 1234;

public Server() {
    super("Server");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(new Dimension(400, 300));
    Container c = getContentPane();
    c.setBackground(Color.BLACK);
    TA = new JTextArea();
    TA.setEditable(true);
    TA.setBackground(Color.BLACK);
    TA.setForeground(Color.GREEN);
    c.add(new JScrollPane(TA));
    setVisible(true);
    TA.append("Server started.");
}

public void start() {
    try {
        TA.append("ServerSocket created. \n Listering for connections...");
        server = new ServerSocket(port, 2);

        for (int i = 0; i < 2; i++) {
            conn = server.accept();
            TA.append("User " + i + " connected");
        }
    } catch (IOException ioe) {
        System.out.println(ioe);
    }
}
}

import javax.swing.SwingUtilities;

`

 public class ServerLauncher {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable(){

        public void run() {
            Server s = new Server();
    //  s.start();
        }

    });

}

}

我正在关注一个youtube指南来写这个,它得到了同样的错误,但看起来它已经在第3部分后停止了所以我从来没有找到如何修复它。 https://www.youtube.com/watch?v=YqTB-S_5mss

2 个答案:

答案 0 :(得分:2)

  • 来自invoking Server s = new Server(); SwingUtilities.invokeLater(new Runnable()的{​​{1}} public class ServerLauncher {错误的想法,您无法从Workers Thread启动难以运行的长代码invokeLaterEvent Dispatch Thread的通知程序,Oracle tutorial Concurency in Swing中需要更多start()来自SwingWorkerRunnable#ThreadThread

  • TA.append("User " + i + " connected");您({再次)Concurency in Swing时遇到问题,Worker Thread的所有更新都必须在EDT上完成,包装到invokeLater()

    < / LI>
  • Runnable#Thread开始Server s = new Server();然后在内圈到TA.append("User " + i + " connected");包裹成invokeLater

答案 1 :(得分:1)

它发生的原因是因为在AWT事件调度程序线程中安排,并且您使用accept方法阻止它,所以没有任何显示。

请改为尝试:

public static void main(String[] args) {
    Server s = new Server();
    s.start();
}


public Server() {
    super("Server");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(new Dimension(400, 300));
    Container c = getContentPane();
    c.setBackground(Color.BLACK);
    TA = new JTextArea();
    TA.setEditable(true);
    TA.setBackground(Color.BLACK);
    TA.setForeground(Color.GREEN);
    c.add(new JScrollPane(TA));
    setVisible(true);
    log(TA, "Server started.");
}

public void log(JTextArea txt, String message) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                txt.append(message);
            }});
}

public void start() {
    try {
        log(TA, "ServerSocket created. \n Listering for connections...");
        server = new ServerSocket(port, 2);

        for (int i = 0; i < 2; i++) {
            conn = server.accept();
            log(TA, "User " + i + " connected");
        }
    } catch (IOException ioe) {
        System.out.println(ioe);
    }
}