import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Headlines extends JFrame{
HeadlinePane news=new HeadlinePane();
public Headlines(){
super("Headline");
setSize(400,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane=new JPanel();
pane.setLayout(new GridLayout(1,1,15,15));
pane.add(news);
setContentPane(pane);
show();
news.scroll();
}
public static void main(String[] arguments){
Headlines head=new Headlines();
}
}
class HeadlinePane extends JPanel{
int y;
String[] headlines={
"Yesterday",
"Beatles,1985",
"Yesterday all my troubles seemd so far away",
"Now it look as through they're here to stay",
"Oh,I believe in yesterday",
"Suddenly,I'm not half the man I used to be"
};
void scroll(){
while(true){
y=y-5;
if(y<0);
y=201;
repaint();
try{
Thread.sleep(1000);
}catch(InterruptedException e){}
}
}
public void paint(Graphics comp){
Graphics2D com2D=(Graphics2D)comp;
Font type=new Font("monospaced",Font.BOLD,14);
com2D.setFont(type);
com2D.setColor(getBackground());
com2D.fillRect(0,0,getSize().width,getSize().height);
com2D.setColor(Color.black);
for(int i=0;i<headlines.length;i++)
com2D.drawString(headlines[i],5,y+(20*i));
}
}
运行结果不是我想要得:我想得到一个滚动显示的文本
谢谢了