我的GUI不会打开,但仍然会出现堆栈溢出

时间:2016-04-20 00:34:37

标签: java

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ObtainData extends Thread {
    private ServerSocket serverSocket;
    int i = 0;
    JPanel jpanel = new JPanel();
    public ObtainData(int port) throws IOException {
        serverSocket = new ServerSocket(port);
        serverSocket.setSoTimeout(900000);
    }

    public void run() {

        while (true) {
            try {
                System.out.println("Starting connection");
                Socket server = serverSocket.accept();
                BufferedImage img = ImageIO.read(ImageIO
                        .createImageInputStream(server.getInputStream()));
                addImage(jpanel, img);
                jpanel.setVisible(true);
            } catch (SocketTimeoutException s) {
                System.out.println("Socket timed out!");
                break;
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
        }
    }

    private void addImage(JComponent parent, BufferedImage img) {
        JLabel jLabel = new JLabel(new ImageIcon(img));
        jLabel.setBounds(parent.getBounds());
        parent.add(jLabel);
        parent.setVisible(true);
    }

    public static void main(String[] args) {
        int port = Integer.parseInt(args[0]);
        try {
            Thread t = new ObtainData(port);
            t.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

就像标题所说,当我运行这段代码时,我得到了堆栈溢出,但是,GUI甚至都不会出现。谁知道为什么?我调查了一下,然而我能想出的任何东西都没有引导我走向正确的方向

0 个答案:

没有答案