【问题标题】:Linebreak in JTextAreaJTextArea 中的换行符
【发布时间】:2016-07-02 09:56:21
【问题描述】:

我正在尝试在 JTextArea 中显示文件中的文本。问题是,JTextArea 不显示任何换行符。如果我尝试以这样的方式显示文本:`

FileHandler fh = new FileHandler();                 
String text = fh.loadFile("src/Files/info.txt");              

textArea = new JTextArea(text);
textArea.setSize(350, 350);
textArea.setVisible(true);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setBorder(null);

this.add(textArea);

text 字符串的内容是Line one\nLine two\n Line three。 TextArea 仅显示以下输出: Line one\nLine two\n Line three

但是如果我像这样手动设置文本:

String text = "Line one\nLine two\n Line three"`

换行符显示正确。

【问题讨论】:

  • 当心转义换行符(你不要在编程语言之外使用“\n”,你实际上使用了换行符)
  • 要么 info.txt 有反斜杠而不是实际的换行符,要么 FileHandler.loadFile 将换行符转换为反斜杠 -n 序列。

标签: java jtextarea line-breaks


【解决方案1】:

有几种方法可以实现这一点,但我发现最简单的方法是用系统换行符替换换行符,如下所示:

text = text.replaceAll("\\\\n", System.getProperty("line.separator"));

所以不是将 \n 打印为字符串,而是在遇到指定的系统换行符时替换那些字符串,这是我的示例。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;


    public class StackQuestions {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            try {
                JPanel middlePanel = new JPanel();
                middlePanel.setBorder(new TitledBorder(new EtchedBorder(), "Display Area"));
                String file =readFile("file.txt");


                // create the middle panel components
                JTextArea display = new JTextArea(16, 58);
                display.setEditable(false); // set textArea non-editable
                display.setText(file);
                JScrollPane scroll = new JScrollPane(display);
                scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

                //Add Textarea in to middle panel
                middlePanel.add(scroll);

                // My code
                JFrame frame = new JFrame();
                frame.add(middlePanel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            } catch (IOException ex) {
                Logger.getLogger(StackQuestions.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        static String readFile(String fileName) throws IOException {
            BufferedReader br = new BufferedReader(new FileReader(fileName));
            try {
                StringBuilder sb = new StringBuilder();
                String line = br.readLine();

                while (line != null) {
                   line = line.replaceAll("\\\\n", System.getProperty("line.separator"));
                    sb.append(line);
                    //sb.append("\n");
                    line = br.readLine();
                }
                return sb.toString();
            } finally {
                br.close();
            }
        }
    }

无论如何我希望这会有所帮助:)

【讨论】:

    【解决方案2】:

    如果您可以更改文件的内容,则可以改用JTextPane 并将您的文本格式化为HTML

    example.html:

    <!DOCTYPE html>
    <html>
    <body>
    
    <h1>My Awesome heading</h1>
    
    <p>This text is so<br>awesome it requires multiple<br>line<br><br>breaks!</p>
    
    </body>
    </html>
    

    记得将内容类型设置为text/html

    FileHandler fh = new FileHandler();                 
    String text = fh.loadFile("example.html");
    
    JTextPane text = new JTextPane();
    text.setContentType("text/html");
    text.setText(text);        
    

    您将在JFrame 中获得以下信息:

    我的真棒标题

    这段文字太棒了
    需要多行

    换行符!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      相关资源
      最近更新 更多