簡體   English   中英

如何使Java GUI settext識別二維數組輸入的新行

[英]How to make Java GUI settext recognize new lines for a 2 dimensional array input

我有一個char [xSize] [ySize]發送到我的GUI。 GUI將長字符串打印為一條長行,而不會識別其中的新行。 我可以使用什么方法或代碼? 我當前使用的代碼是:

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

提前致謝

對於JLabel ,您需要先用<br>替換新行( \\n ),然后將文本包裝在<html>...</html>

String Sentence = new String(receivePacket.getData());  

//Print received text to GUI:
label.setText("<html>" + Sentence.replaceAll("\n", "<br>") + "</html>");

或者,您也可以改為使用不可編輯的JTextArea ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM