JOptionPane: parentComponent does not have a valid parent

拜拜、爱过 提交于 2020-01-24 20:28:49

问题


I am trying to compile this code but getting run time error this chat system using socket programming, server started successfully but when I run ClientChatApp then I'm getting

java.lang.RuntimeException : JOptionPane: parentComponent does not have a valid parent

ChatappServer

package chatappserver;

import java.net.*;
import java.io.*;
import java.util.Vector;
import java.lang.*;

public class ChatappServer {

  private static int port = 1234;
  private static ServerSocket server = null;
  private static Socket clientSocket;
  private static String line;
  private static BufferedReader streamIn;
  private static PrintStream streamOut;


  private static Vector < String > usernames = new Vector < String > ();
  private static Vector < PrintStream > streams = new Vector < PrintStream > ();

  public static void main(String[] args) throws IOException {

    try {
      System.out.println("Connecting to port " + port + " ....");
      server = new ServerSocket(port);
      System.out.println("Chat application server is now running..");
      while (true) {
        clientSocket = server.accept();
        chatHandler c = new chatHandler(clientSocket);
        c.start();
      }
    } catch (IOException e) {
      System.out.println("Couldn't connect to the port!");
    } finally {
      server.close();
    }
  }

  private static class chatHandler extends Thread {

    private Socket clientSocket;

    public chatHandler(Socket clientSocket) {
      super("chatHandler");
      this.clientSocket = clientSocket;
    }

    public void run() {

      try {
        streamIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        streamOut = new PrintStream(clientSocket.getOutputStream(), true);

        while (true) {
          streamOut.println("Username");
          String line = streamIn.readLine();
          if (line == null) {
            return;
          }
          try {
            synchronized(usernames) {
              if (!usernames.contains(line)) {
                usernames.add(line);
                break;
              }
            }
          } catch (Exception e) {
            System.out.println(e);
          }
        }
        streamOut.println("Welcome");
        streams.add(streamOut);

        while (true) {
          String message = streamIn.readLine();
          if (message == null) {
            return;
          }
          for (PrintStream stream: streams) {
            stream.println("From " + line + ": " + message);
          }

        }
      } catch (IOException e) {
        System.out.println(e);

      } finally {
        if (line != null && streamOut != null) {
          usernames.remove(line);
          streams.remove(streamOut);
        }
        try {
          clientSocket.close();
        } catch (IOException e) {
          System.out.println(e);
        }

      }


    }


  }

}

ChatappClient

package chatappclient;

import java.net.*;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.event.*;

public class ChatappClient {

  private static int port = 1234;
  JFrame window = new JFrame("Chat");
  JButton sendBox = new JButton("Send");
  JTextField inputMsg = new JTextField(35);
  JTextArea outputMsg = new JTextArea(10, 35);
  private static BufferedReader streamIn;
  private static PrintStream streamOut;

  public static void main(String[] args) throws Exception {
    ChatappClient client = new ChatappClient();
    client.window.setVisible(true);
    client.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    client.run();
  }

  public ChatappClient() {

    inputMsg.setSize(40, 20);
    sendBox.setSize(5, 10);
    outputMsg.setSize(35, 50);
    inputMsg.setEditable(false);
    outputMsg.setEditable(false);
    window.getContentPane().add(inputMsg, "South");
    window.getContentPane().add(outputMsg, "East");
    window.getContentPane().add(sendBox, "West");
    window.pack();
    sendBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        streamOut.println(inputMsg.getText());
        inputMsg.setText("");
      }
    });
    inputMsg.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        streamOut.println(inputMsg.getText());
        inputMsg.setText("");
      }
    });
  }

  private String getUsername() {
    return JOptionPane.showInternalInputDialog(window, "Server IP Address:", "Welcome to Chat", JOptionPane.QUESTION_MESSAGE);
  }

  private void run() throws IOException {
    Socket clientSocket = new Socket("localhost", port);
    streamIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    streamOut = new PrintStream(clientSocket.getOutputStream(), true);

    while (true) {
      String line = streamIn.readLine();
      if (line.startsWith("Username")) {
        streamOut.println(getUsername());
      } else if (line.startsWith("Welcome")) {
        inputMsg.setEditable(true);
      } else if (line.startsWith("From")) {
        outputMsg.append(line.substring(10) + "\n");
      }
    }
  }
}

Getting Following Exception:

Exception in thread "main" java.lang.RuntimeException: JOptionPane: parentComponent does not have a valid parent
    at javax.swing.JOptionPane.createInternalFrame(Unknown Source)
    at javax.swing.JOptionPane.showInternalInputDialog(Unknown Source)
    at javax.swing.JOptionPane.showInternalInputDialog(Unknown Source)
    at com.nody.ChatappClient.getUsername(ChatappClient.java:55)
    at com.nody.ChatappClient.run(ChatappClient.java:66)
    at com.nody.ChatappClient.main(ChatappClient.java:26)

回答1:


showInternalInputDialog is for option panes meant to appear in a JDesktopPane. Look instead to showInputDialog.



来源:https://stackoverflow.com/questions/28655589/joptionpane-parentcomponent-does-not-have-a-valid-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!