Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JOptionPane:parentComponent没有有效的父级_Java_Swing_Runtimeexception - Fatal编程技术网

Java JOptionPane:parentComponent没有有效的父级

Java JOptionPane:parentComponent没有有效的父级,java,swing,runtimeexception,Java,Swing,Runtimeexception,我试图编译这段代码,但运行时出错。这个聊天系统使用套接字编程,服务器启动成功,但当我运行ClientChatApp时,我得到了 java.lang.RuntimeException : JOptionPane: parentComponent does not have a valid parent 查塔普服务器 获取以下异常: Exception in thread "main" java.lang.RuntimeException: JOptionPane: parentComponent

我试图编译这段代码,但运行时出错。这个聊天系统使用套接字编程,服务器启动成功,但当我运行ClientChatApp时,我得到了

java.lang.RuntimeException : JOptionPane: parentComponent does not have a valid parent
查塔普服务器 获取以下异常:

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)

showInternalInputDialog用于显示在JDesktopPane中的选项窗格。查看showInputDialog。

showInternalInputDialog用于显示在JDesktopPane中的选项窗格。查看showInputDialog。

1请注意,“运行此代码”功能仅适用于HTML和JavaScript,而此代码两者都不适用。使用{}按钮来指示Java代码块和堆栈跟踪。2见“是”。此建议也适用于设置大小。。。按钮、文本字段和文本区域的大小应根据组件的字体以及文本字段和文本区域的行/列的大小来确定。可以使用setMargin放大按钮。请尝试使用。不要使用JOptionPane.showInternalInputDialog,窗口不是JInternalFrame,而是使用JOptionPane.showInputDialog1。请注意,“运行此代码”功能仅适用于HTML和JavaScript,而此代码两者都不适用。使用{}按钮来指示Java代码块和堆栈跟踪。2见“是”。此建议也适用于设置大小。。。按钮、文本字段和文本区域的大小应根据组件的字体以及文本字段和文本区域的行/列的大小来确定。可以使用setMargin.放大按钮。请尝试使用。不要使用JOptionPane.showInternalInputDialog,窗口不是JInternalFrame,请使用JOptionPane.showInputDialog
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");
      }
    }
  }
}
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)