2013-04-11 46 views
0

Java新手。我試圖顯示滾動文本使用Jpanel和計時器,它的工作原理,但我試圖插入一個行分隔符使用系統行分隔文本顯示沒有換行符,爲什麼?Java - 在滾動文本中插入換行符

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 2013-04-11 03:50:30

+1

請參見[本文](http://stackoverflow.com/questions/4413132/problems-with-newline-in-graphics2d -drawstring) – Reimeus 2013-04-11 03:57:37

回答

1

換行符(\ n)的僅適用於控制檯寫作。它不起作用。只需使用單獨的繪圖方法來編寫不同的文本。

問候, 拉維