2016-07-02 6 views
0

ファイルのテキストをJTextAreaに表示しようとしています。問題は、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"` 

改行が正しく表示されています。

+0

エスケープされた改行には注意してください(プログラミング言語の外では「\ n」は使用しません。実際に改行を使用します) –

+0

info.txtには実際の改行の代わりにバックスラッシュnがあります。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に次のように取得します:

このテキスト

を見出しマイ恐ろしいので
素晴らしいそれは、複数の
ラインに

休憩が必要です!