我正在尝试实现一个程序,客户端将向服务器端发送修复消息,而我在服务器端的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 :(得分:6)
您将textArea
添加到JScrollPane
,然后再对窗格执行任何操作。
jScrollPane1 = new JScrollPane(tfFIXMsg);
您需要add(jScrollPane1);