import java.awt.*;
import javax.swing.*;
import java.util.*;
class jframe extends JFrame
{
private jPanel jpanel;
public jframe()
{
super("ScrollDemo");
jpanel = new jPanel();// //这
setContentPane(jpanel);
setSize(400,300);// //这
show();
jpanel.scroll(); //这句话为什么不能放在上面标示的位置|?
}
public static void main(String[] args)throws Exception
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new jframe();
}
}
class jPanel extends JPanel
{
private String[] str = {
"Yesterday",
"Beatles, 1965",
" ",
"Yesterday all my troubles seemed so far away",
"Now it looks as though they're here to stay",
"Oh I believe in yesterday",
"Suddenly, I'm not half the man I used to be",
"There's shadow hanging over me",
"Oh Yesterday came suddenly",
"Why she had to go I don't know",
"She wouldn't say",
"I said something wrong now I long for yesterday",
"Yesterday love was such an easy game to play",
"Now I need a place to hideaway",
"Ho I believe in yesterday",
"Why she had to go I don't know",
"She wouldn't say",
"I said something wrong now I long for yesterday",
"Yesterday love was such an easy game to play",
"Now I need a place to hideaway",
"Ho I believe in yesterday"
};
private int number = 201;
public void scroll()
{
while(true)
{
number = number-1;
if(number <- 200)
{
number = 201;
}
repaint();
try
{
Thread.sleep(50);
}
catch(InterruptedException e)
{
}
}
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
Font type = new Font("monospaced",Font.BOLD,14);
g2.setFont(type);
g2.setColor(getBackground());
g2.fillRect(0,0,getSize().width,getSize().height);
g2.setColor(Color.black);
for(int i = 0;i < str.length;i++)
g2.drawString(str[i],5,number + (20*i));
}
}
我在上面已经标注好了`