2012-02-20 17 views
1

String에 많은 양의 텍스트를 추가하려고합니다. 이를 위해 필자는 텍스트 내에 새로운 줄을 써서 인쇄 할 때 모든 것이 직선으로 나타나지 않도록해야합니다. 어떻게 문자열 텍스트 안에 새로운 라인을 넣을 수 있습니까?문자열 텍스트 내의 줄 바꿈?

String writing = "7 Dear friends, let us love one another, for love comes from God. Everyone who loves has been born of God and knows God. 8 Whoever does not love does not know God, because God is love. 9 This is how God showed his love among us: He sent his one and only Son into the world that we might live through him. 10 This is love: not that we loved God, but that he loved us and sent his Son as an atoning sacrifice for our sins. 11 Dear friends, since God so loved us, we also ought to love one another. 12 No one has ever seen God; but if we love one another, God lives in us and his love is made complete in us. 1 Everyone who believes that Jesus is the Christ is born of God, and everyone who loves the father loves his child as well. 2 This is how we know that we love the children of God: by loving God and carrying out his commands. 3 In fact, this is love for God: to keep his commands. And his commands are not burdensome, 4 for everyone born of God overcomes the world. This is the victory that has overcome the world, even our faith. 5 Who is it that overcomes the world? Only the one who believes that Jesus is the Son of God."; 

    JTextArea sampleWriting = new JTextArea(); 
    sampleWriting.setText(writing); 
    sampleWriting.setFocusable(false); 

    JTextField userTypingRegion = new JTextField(); 

    dataCollectionRegion.add(sampleWriting); 
    dataCollectionRegion.add(userTypingRegion); 

I 시도/N하지만 JTextArea에에, 쓰기가 모두 한 줄에 있습니다

이 내 코드입니다.

답변

6

개행 문자 사용하십시오

String x = "line 1\nline 2"; 
6

을이 :

StringBuilder sb = new StringBuilder("long string"); 
// adds a line separator 
sb.append(System.getProperty("line. separator")); 
sb.append("another long string"); 
// ... more lines ... 
String result = sb.toString(); 

위의 코드의 좋은 부분은 추가 행 분리 자신이 위치한 플랫폼에 대한 올바른 때문이다 (리눅스, 윈도우 등)

편집 :

,536,

코드를 게시 했으므로, 나는 그다지 긴 문자열이 아님을 알 수 있습니다 :). 매우 간단합니다. 다음을 시도하십시오.

String writing = "Hello. \nWorld. \nLamp."; 
+0

+1,'StringBuilder'와'line.separator'. – RanRag

+0

죄송합니다. 저는이 코드를 내 코드에 적용하는 법을 배웠습니다. – Maydayfluffy

+0

그럼 글씨는 오랫동안 그다지 길지 않습니다. 다음을 사용하십시오 :'String writing = "Hello. \ nWorld. \ nLamp."; ' –