0

Java新手。我正在尝试使用 Jpanel 和计时器显示滚动文本,它可以工作,但我尝试使用系统行分隔符插入行分隔符,文本显示时没有换行符,为什么?

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

}

4

1 回答 1

1

换行符(\n) 仅适用于控制台写入。它在摆动时不起作用。只需使用单独的 Draw 方法来编写不同的文本。

问候,拉维

于 2013-04-11T04:14:06.247 回答