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));
}
}
运行结果不是我想要得:我想得到一个滚动显示的文本
谢谢了
----------------解决方案--------------------------------------------------------
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);
setVisible(true);
}
public static void main(String[] arguments){
Headlines head=new Headlines();
}
}
class HeadlinePane extends JPanel implements Runnable{
int y;
public HeadlinePane(){
new Thread(this).start();
}
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"
};
public void run(){
while(true){
y-=1;
if(y<0)//这里不能有分号,否则前面的赋值语句写了跟没写一样
y=201;
repaint();
try{
Thread.sleep(10);
}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));
}
}
我已经帮你改过来了,要实现动画功能一般都是要用线程的
然后为了使你的动画更增滑,我减少了休眠的时间,并且像素每次减少一个
还有,你的程序中有一个很小的错误,那就是if(y<0)后面加一个分号,以后千万要小心
----------------解决方案--------------------------------------------------------
行了行了,哈哈
谢谢了,java,my love
----------------解决方案--------------------------------------------------------
import java.util.*;引入这个好象没有用啊
----------------解决方案--------------------------------------------------------
y=201;
com2D.drawString(headlines[i],5,y+(20*i));
这两句是怎么实现字符的动态流动的能解释一下吗?先谢谢了
----------------解决方案--------------------------------------------------------
因为在线程的run里面,它每次都会使y改变,然后调用repaint()方法,此方法会调用paint再画一次
再画一次的时候,里面的y坐标是每次都会变的,这样就形成了一种动画效果
----------------解决方案--------------------------------------------------------