2013-04-11 2 views
0

Java Newbie. Jpanel 및 타이머를 사용하여 스크롤 텍스트를 표시하려고하는데 작동하지만 시스템 줄을 사용하여 줄 구분 기호를 삽입하려했으나 줄 바꿈없이 텍스트 표시가 왜 나타나지 않습니까? (\ n)도자바 - 스크롤 텍스트에 줄 바꿈 삽입

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Toolkit; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 


public class Participants extends JPanel 
{ 
    private int x; 
    private int y; 
    private String text; 

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 

    public Participants () 
    { 
     x = 600; 
     y = 1200; 
     String eol = System.getProperty("line.separator"); 

     text = "Story Director/Producer:"+eol+"Mr. SMith" + 
      "Technical Director:" + eol + 
       "Mr. T" + eol + eol; 

     setSize(1200, 900); 
    } 

public void paint(Graphics g) 
{ 
    super.paint(g); 
    g.setColor(Color.white); 
    g.fillRect(0, 0, 1200, 900); 
    g.setColor(Color.black); 
    g.drawString(text,x, y); 


} 

public void start() throws InterruptedException 
{ 
    while(true) 
    { 
     while(y >= 0) 
     { 
      x = getWidth()/2; 
      y--; 
      repaint(); 
      Thread.sleep(10); 
     } 
    } 
} 

public static void main (String [] args) throws InterruptedException 
{ 
    JFrame frame = new JFrame("Participants "); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    Participants participants = new Participants(); 
    frame.getContentPane().add(participants ); 
    frame.setSize(1200, 900); 
    frame.setVisible(true); 
    participants .start(); 
} 

}

+1

는'JTextArea' 또는 동등한를 사용합니다. 새로운 줄 문자를 지원한다는 점 외에도, 그림을 그릴 때 매우 최적화되어 있습니다. – MadProgrammer

+1

[이 게시물보기] (http://stackoverflow.com/questions/4413132/problems-with-newline-in-graphics2d) -drawstring) – Reimeus

답변

1

줄 바꿈은 콘솔 쓰기 위해 작동합니다. 스윙에서는 작동하지 않습니다. 별도의 Draw 메서드를 사용하여 다른 텍스트를 작성하면됩니다.

감사합니다, 라비