[英]How to make Java GUI settext recognize new lines for a 2 dimensional array input
I have a char[xSize][ySize] being sent to my GUI. 我有一个char [xSize] [ySize]发送到我的GUI。 The GUI prints the long string as one long line without recognizing the new lines in it.
GUI将长字符串打印为一条长行,而不会识别其中的新行。 What method or code could I use?
我可以使用什么方法或代码? The code I currently use is:
我当前使用的代码是:
JFrame frame1 = new JFrame("Start Screen");
JLabel label = new JLabel();
frame1.setVisible(true);
frame1.setSize(500, 500);
frame1.setLayout(new BorderLayout());
frame1.add(label, BorderLayout.NORTH);
//Code to recieve from other program:
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
String Sentence = new String(receivePacket.getData());
//Print received text to GUI:
label.setText(Sentence);
Thanks in advance 提前致谢
For something JLabel
, you would need to first replace the new lines ( \\n
) with <br>
and wrap the text in <html>...</html>
对于
JLabel
,您需要先用<br>
替换新行( \\n
),然后将文本包装在<html>...</html>
String Sentence = new String(receivePacket.getData());
//Print received text to GUI:
label.setText("<html>" + Sentence.replaceAll("\n", "<br>") + "</html>");
Or you might be able to use a non-editable JTextArea
instead... 或者,您也可以改为使用不可编辑的
JTextArea
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.