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));
}
}
我在上面已经标注好了`
----------------解决方案--------------------------------------------------------
这个很简单,因为scroll这个方法是一个无限循环的方法,他永远不会返回,直到程序结果为止
所以如果放到上面去,那么他就永远不会执行下一次,那么你将什么也看不到了
如果你在scroll方法里面起一个线程去处理重画的事的话,那么你放哪里都可以了
----------------解决方案--------------------------------------------------------