gpt4 book ai didi

java - 为什么我的文本区域不显示

转载 作者:行者123 更新时间:2023-12-01 08:10:19 25 4
gpt4 key购买 nike

我正在尝试实现一个程序,其中客户端将向服务器端发送修复消息,并且服务器端的 UI 将在文本区域上显示修复消息。但是,当我启动程序时,文本字段和所有标签都会显示,但文本区域除外。当我尝试从客户端发送消息时,除了文本区域之外,一切都正常。

代码

    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();
}
}

最佳答案

textArea 添加到 JScrollPane,然后不对 Pane 执行任何操作。

jScrollPane1 = new JScrollPane(tfFIXMsg);

您需要add(jScrollPane1);

关于java - 为什么我的文本区域不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18003049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com