| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 449 人关注过本帖
标题:一个不明白但又常见的问题
只看楼主 加入收藏
shiyide
Rank: 2
等 级:新手上路
威 望:4
帖 子:297
专家分:0
注 册:2006-2-22
收藏
 问题点数:0 回复次数:1 
一个不明白但又常见的问题

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

}
}
我在上面已经标注好了`

2006-06-18 07:59
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
这个很简单,因为scroll这个方法是一个无限循环的方法,他永远不会返回,直到程序结果为止
所以如果放到上面去,那么他就永远不会执行下一次,那么你将什么也看不到了
如果你在scroll方法里面起一个线程去处理重画的事的话,那么你放哪里都可以了

可惜不是你,陪我到最后
2006-06-18 10:16
快速回复:一个不明白但又常见的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015991 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved