Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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 GUI试图从多个文本字段获取多个输入_Java_Swing_User Interface_Io - Fatal编程技术网

java GUI试图从多个文本字段获取多个输入

java GUI试图从多个文本字段获取多个输入,java,swing,user-interface,io,Java,Swing,User Interface,Io,如何从您创建的多个文本字段中获取多个输入? 我想要一个文本字段来获取端口号,另一个文本字段来获取文件位置。 因此,一旦用户输入int和字符串,我就可以将这些输入用于程序 我是新手,所以当我尝试实现它时,我会输入端口号,突然UI似乎“冻结”,我无法输入文件位置 框架构造 public TcpServerCompareCSV () { setLayout(new FlowLayout()); // "this" Frame sets its layout to F

如何从您创建的多个文本字段中获取多个输入? 我想要一个文本字段来获取端口号,另一个文本字段来获取文件位置。 因此,一旦用户输入int和字符串,我就可以将这些输入用于程序

我是新手,所以当我尝试实现它时,我会输入端口号,突然UI似乎“冻结”,我无法输入文件位置

框架构造

   public TcpServerCompareCSV () {
      setLayout(new FlowLayout());
         // "this" Frame sets its layout to FlowLayout, which arranges the components
         //  from left-to-right, and flow to next row from top-to-bottom.

      lblPort = new Label("Port");          // construct Label
      add(lblPort);                         // "this" Frame adds Label

      tfPort = new TextField("0", 10);  // construct TextField
      tfPort.setEditable(true);         //edit text
      add(tfPort);                      // "this" Frame adds tfCount
      tfPort.addActionListener(this);   // for event-handling



      lblLocation = new Label("CSV File Location"); // construct Label
      add(lblLocation);                             // "this" Frame adds Label

      tfLocation = new TextField("text", 40);       // construct TextField
      tfLocation.setEditable(true);                 //edit text
      add(tfLocation);                              // "this" Frame adds tfCount
      tfLocation.addActionListener(this);


      setTitle("compare");      // "this" Frame sets title
      setSize(250, 100);        // "this" Frame sets initial window size
      setVisible(true);         // "this" Frame shows


      addWindowListener(this);
        // "this" Frame fires WindowEvent its registered WindowEvent listener
        // "this" Frame adds "this" object as a WindowEvent listener

   }
动作事件

   /** ActionEvent handler - Called back when user clicks the button. */
   @Override
   public void actionPerformed(ActionEvent evt) {
    // Get the String entered into the TextField tfPort, convert to int
      port = Integer.parseInt(tfPort.getText());


      fileLocation = tfLocation.getText();
      String csvName = fileLocation;








  ServerSocket serverSocket = null; 

  try { 
       serverSocket = new ServerSocket(port); 
      } 
  catch (IOException e) 
      { 
       System.err.println("Could not listen on port: 57635."); 
       System.exit(1); 
      } 

  Socket clientSocket = null; 
  System.out.println ("Waiting for connection.....");

  try { 
       clientSocket = serverSocket.accept(); 
      } 
  catch (IOException e) 
      { 
       System.err.println("Accept failed."); 
       System.exit(1); 
      } 

  System.out.println ("Connection successful");
  System.out.println ("Waiting for input.....");

  PrintWriter out = null;
try {
    out = new PrintWriter(clientSocket.getOutputStream(), 
                                        true);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} 
  BufferedReader in = null;
try {
    in = new BufferedReader( 
              new InputStreamReader( clientSocket.getInputStream()));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} 

  String inputLine, outputLine;

  try {
    while ((inputLine = in.readLine()) != null) 
          { 
           System.out.println ("Server: " + inputLine); 



           if (inputLine.trim().equals("Bye.")) {
               System.out.println("Exit program"); 
               break;
               } 

           Scanner input1 = new Scanner(new File(csvName));
           Scanner input2 = new Scanner(new File(csvName));
           Scanner input3 = new Scanner(new File(csvName));
           Scanner input4 = new Scanner(new File(csvName));


           String csvline = getCsvLineVal (getLocation34CSV(getTag34Value(Tag34Location(getTagCSV( parseFixMsg(inputLine ,inputLine))), getValueCSV( parseFixMsg(inputLine ,inputLine))), getVal34(input1,  input2)), getCSVLine( input3,  input4) );
           outputLine = compareClientFixCSV( getTagCSV( parseFixMsg(inputLine ,inputLine)), getValueCSV(parseFixMsg(inputLine ,inputLine)), getCSVTag(csvline), getCSVValue(csvline));

           out.println(outputLine);

           input1.close();
           input2.close();
           input3.close();
           input4.close();



          }
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}


  out.close(); 
  try {
    in.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} 
  try {
    clientSocket.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} 
  try {
    serverSocket.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}




   }

仅调用addActionListener(此)对于您的按钮,不要侦听文本字段上的操作,因为当在文本字段上发生操作时(用户点击enter),将调用
actionPerformed
方法。我猜您在第一个字段之后执行此操作,并且您的操作方法被此字段调用,然后等待套接字连接,因为这是一个阻塞调用,这将使您的GUI无响应。

仅调用
addActionListener(此)
对于您的按钮,不要侦听文本字段上的操作,因为当在文本字段上发生操作时(用户点击enter),将调用
actionPerformed
方法。我猜您在第一个字段之后会这样做,并且您的操作方法会被该字段调用,然后等待套接字连接,因为这是一个阻塞调用,这会使您的GUI没有响应。

如果我没记错的话,通过将
ActionListener
添加到两个文本字段,一旦你在其中一个游戏中按回车键,他们将立即启动该游戏。 这将立即触发您的网络代码

您应该只注册按钮的操作

此外,正如已经在评论中指出的,您正在GUI线程上执行网络通信,这导致了冻结


在操作实现中,必须生成一个单独的线程来执行实际的网络通信,以防止阻塞。要了解其工作原理,请查看
Runnable
和Executor框架的文档。

通过将
ActionListener
添加到两个文本字段中,如果我没记错的话,只要您在其中一个字段中单击Return,它们就会触发事件。 这将立即触发您的网络代码

您应该只注册按钮的操作

此外,正如已经在评论中指出的,您正在GUI线程上执行网络通信,这导致了冻结


在操作实现中,必须生成一个单独的线程来执行实际的网络通信,以防止阻塞。要了解其工作原理,请查看
Runnable
和Executor框架的文档。

actionPerformed
中,您似乎正在执行许多耗时的任务。这些任务在主线程上执行,这确实会导致GUI冻结。您应该研究如何在后台线程上执行这些任务。在
actionPerformed
中,您似乎正在执行许多耗时的任务。这些任务在主线程上执行,这确实会导致GUI冻结。您应该研究如何在后台线程上执行这些任务。