2013-07-31 53 views
0

如何從您創建的多個文本字段獲取多個輸入? 我想要一個文本字段獲取端口號,另一個獲取文件位置。 所以一旦用戶輸入int和字符串,我可以使用這些輸入的程序。java GUI嘗試從多個文本字段獲取多個輸入

我對此很陌生,所以當我試圖實現這一點時,我會輸入端口號,並突然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(); 
} 




    } 
+1

看來你正在'actionPerformed'中執行很多耗時的任務。這些任務在主線程上執行,這確實會導致您的GUI凍結。您應該研究如何在後臺線程上執行這些任務。 –

回答

2

通過將ActionListener添加到兩個文本字段中,如果我沒有記錯的話,只要您點擊其中一個的Return,就會觸發事件。 這將立即觸發您的網絡代碼。

您應該只註冊該按鈕的動作。

此外,正如在評論中已經指出的,您正在GUI線程上執行網絡通信,導致凍結。

在操作實現中,您必須生成一個單獨的線程來執行實際的網絡通信以防止阻塞。要了解它的工作方式,請查看Runnable和Executor框架的文檔。

1

只有打電話addActionListener(this);您的按鈕,不聽你的文本框的行動,因爲行動發生時被調用的方法actionPerformed在你的文本區域(用戶點擊輸入)。我猜你在第一個字段後面這樣做,並且你的動作方法被這個調用,然後等待套接字連接,因爲這是一個阻塞調用,這會讓你的GUI沒有響應。

相關問題