2016-07-02 9 views
0

JTextArea에있는 파일의 텍스트를 표시하려고합니다. 문제는 JTextArea에 줄 바꿈이 표시되지 않는다는 것입니다. 나는이 같은 방법으로 텍스트를 표시하려고하면`text 문자열의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); 

콘텐츠 Line one\nLine two\n Line three입니다. 텍스트 영역은 다음과 같은 출력을 보여줍니다 Line one\nLine two\n Line three

을하지만 수동으로 텍스트를 다음과 같이 설정 한 경우 :

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

바꿈이 제대로 표시됩니다.

+0

줄 바꿈에주의하십시오 (프로그래밍 언어 이외의 "\ n"은 사용하지 않습니다. 실제로는 줄 바꿈 사용) –

+0

info.txt에는 실제 줄 바꿈 대신 백 슬래시가 포함되어 있거나 FileHandler.loadFile이 줄 바꿈 백 슬래시 -n 시퀀스. –

답변

0

그러나 내가 가장 간단한 이런 시스템 새 라인 문자로 새로운 라인 문자를 대체하는 것이었다 발견,이를 달성하기위한 몇 가지 방법이 있습니다 : 문자열로

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

나는이 어쨌든에 도움이되기를 바랍니다 당신이 파일의 내용을 변경할 수있는 경우 :

0

, 당신은 대신 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에 다음을 얻을 것이다 :

내 굉장이 텍스트가 너무
입니다

을 향하고 최고는 여러에게
라인

휴식이 필요합니다!