有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JTextArea中的java换行符

我试图在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。 文本区域仅显示以下输出: Line one\nLine two\n Line three

但是如果我手动设置文本,如下所示:

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

换行符显示正确


共 (2) 个答案

  1. # 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. # 2 楼答案

    如果可以更改文件的内容,则可以使用JTextPane,并在HTML中格式化文本:

    例如。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中你会得到以下信息:

    我的头球太棒了

    这篇文章太棒了,需要多行符