2013-08-01 47 views
0

我正在嘗試實現一個程序,其中客戶端將修復消息發送到服務器端,並且我爲服務器端的UI將顯示FIX消息文本區域。 但是,當我啓動程序時,除文本區域外,將顯示文本字段和所有標籤。當我嘗試從客戶端發送消息時,除了文本區域外,所有東西都起作用。爲什麼我的文本區域不會顯示

代碼

import java.io.BufferedReader; 
    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.IOException; 
    import java.io.InputStreamReader; 
    import java.io.PrintWriter; 
    import java.net.ServerSocket; 
    import java.net.Socket; 
    import java.util.Scanner; 
    import java.util.StringTokenizer; 
    import java.awt.*;  // using AWT containers and components 
    import java.awt.event.*; // using AWT events and listener interfaces 

    import javax.swing.JScrollPane; 
    import javax.swing.JTextArea; 

    public class TcpServerCompareCSV extends Frame implements ActionListener , WindowListener {   

      private Label lblPort;  // declare component Label 
      private TextField tfPort; // declare component TextField  
      private int port;   // port number 

      private Label lblLocation;  // declare component Label 
      private TextField tfLocation; // declare component TextField  
      private String fileLocation;  // csv file location 

      private Button btnCount; // declare component Button   //________________________________________________________________________________\\ 

      private Label lblCSVLine;  // declare component Label from csv file line, server side 
      private TextField tfCSVLine;  // declare component TextField from csv file line, server side  
      private String CSVLine;   // port number 

      private Label lblFIXMsg;  // declare component Label from csv file line, server side 
      private JTextArea tfFIXMsg; // declare component TextField from csv file line, server side  
      private String FIXMsg;   // port number 
      private JScrollPane jScrollPane1;//________________________________________________________________________________\\ 
      /** WindowEvent handlers */ 
      // Called back upon clicking close-window button 
      @Override 
      public void windowClosing(WindowEvent e) { 
       System.exit(0); // terminate the program 
      } 

      //constructor for frame 
      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", 40); // 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 
      //________________________________________________________________________________\\ 

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

       lblCSVLine = new Label("CSV Line");   // construct Label 
       add(lblCSVLine);        // "this" Frame adds Label 

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

       lblFIXMsg = new Label("FIX message from client");    // construct Label 
       add(lblFIXMsg);        // "this" Frame adds Label 

       tfFIXMsg = new JTextArea();  // construct TextField 
       tfFIXMsg.setColumns(20); 
       tfFIXMsg.setLineWrap(true); 
       tfFIXMsg.setRows(50); 
       tfFIXMsg.setWrapStyleWord(true); 
       tfFIXMsg.setEditable(false);     //edit text 
       add(tfFIXMsg);        // "this" Frame adds tfCount 

       jScrollPane1 = new JScrollPane(tfFIXMsg);   

    add(jScrollPane1); 

       btnCount = new Button("Enter"); // construct Button 
       add(btnCount);     // "this" Frame adds Button 
       btnCount.addActionListener(this); // for event-handling   
       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;     
      /* 
      Scanner console = new Scanner(System.in); 
      System.out.println("Type in CSV file location: "); 
       //String csvName = console.nextLine(); 
       String csvName = "C:\\Users\\I593458\\Downloads\\orders.csv"; 

    */  
      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); 
        tfFIXMsg.setText(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); 
        tfCSVLine.setText(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(); 
     } 
    } 
+0

定義 「不工作」。到目前爲止,您採取了哪些步驟進行調試? –

+0

請張貼SSCE [http://sscce.org/] –

+0

您正在將'JTextPane'添加到ScrollPane,但滾動窗格不會被添加到您的「Frame」中。我猜想將它添加到滾動窗格將其從框架中刪除。 – Robadob

回答

6

您添加textAreaJScrollPane,然後從不跟任何窗格。

jScrollPane1 = new JScrollPane(tfFIXMsg);

你需要add(jScrollPane1);

+0

謝謝!文本區域顯示,但客戶端的文本仍然沒有。 tfFIXMsg.setText(inputLine); –

+0

檢查進入'in'的內容,確保信息正確接收。 –

相關問題