Java编写程序让一个字符串在窗口显示并移动,我写的程序不能运行 谁帮我看看错在哪 谢了 新手求教
importjava.awt.*;classmyCanvasextendsCanvas{//在画布上画字符串"中华..."intx=0,y=0;publicvoidpai...
import java.awt.*;
class myCanvas extends Canvas{//在画布上画字符串"中华..."
int x=0,y=0;
public void paint(Graphics g){
g.drawString("中华人民共和国",x ,y );
}
void move(){
if(x<400)
x++;
else
x--;
}
myCanvas(){
setSize(400,300);
setBackground(Color.CYAN);
}
}
class myThread implements Runnable{//建立1个线程让字符串不断平移
myCanvas draw=new myCanvas();
public void run() {
while(true){
draw.move();
draw.repaint();
}
}
}
public class TickerTape extends Frame{//放画布的窗口
Thread t=new Thread(new myThread());
myCanvas drawing=new myCanvas();
TickerTape(){
super("my window");
setLayout(new BorderLayout());
setSize(400,400);
add("Center",drawing);
t.start();
setVisible(true);
}
public static void main(String args){
new TickerTape();
}
} 展开
class myCanvas extends Canvas{//在画布上画字符串"中华..."
int x=0,y=0;
public void paint(Graphics g){
g.drawString("中华人民共和国",x ,y );
}
void move(){
if(x<400)
x++;
else
x--;
}
myCanvas(){
setSize(400,300);
setBackground(Color.CYAN);
}
}
class myThread implements Runnable{//建立1个线程让字符串不断平移
myCanvas draw=new myCanvas();
public void run() {
while(true){
draw.move();
draw.repaint();
}
}
}
public class TickerTape extends Frame{//放画布的窗口
Thread t=new Thread(new myThread());
myCanvas drawing=new myCanvas();
TickerTape(){
super("my window");
setLayout(new BorderLayout());
setSize(400,400);
add("Center",drawing);
t.start();
setVisible(true);
}
public static void main(String args){
new TickerTape();
}
} 展开
展开全部
你好,一些小错误,我修改了下,修改的地方都有注释,可以直接运行了
import java.awt.*;
class myCanvas extends Canvas {// 在画布上画字符串"中华..."
int x = 0, y = 100;//这里y太小了,改大了点
public void paint(Graphics g) {
g.drawString("中华人民共和国", x, y);
}
void move() {
if (x < 400)
x++;
else
x--;
}
myCanvas() {
setSize(400, 300);
setBackground(Color.CYAN);
}
}
class myThread implements Runnable {// 建立1个线程让字符串不断平移
myCanvas draw;
public myThread(myCanvas draw) {
this.draw = draw;
}
public void run() {
try {
while (true) {
draw.move();
draw.repaint();
Thread.sleep(50);//这里要让它暂停会儿,不然太快没看着就过去了
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class TickerTape extends Frame {// 放画布的窗口
myCanvas drawing = new myCanvas();
Thread t = new Thread(new myThread(drawing));//这里要把你的drawing传过去
TickerTape() {
super("my window");
setLayout(new BorderLayout());
setSize(400, 400);
add("Center", drawing);
t.start();
setVisible(true);
}
public static void main(String[] args) {//这里是String[]啊
new TickerTape();
}
}
import java.awt.*;
class myCanvas extends Canvas {// 在画布上画字符串"中华..."
int x = 0, y = 100;//这里y太小了,改大了点
public void paint(Graphics g) {
g.drawString("中华人民共和国", x, y);
}
void move() {
if (x < 400)
x++;
else
x--;
}
myCanvas() {
setSize(400, 300);
setBackground(Color.CYAN);
}
}
class myThread implements Runnable {// 建立1个线程让字符串不断平移
myCanvas draw;
public myThread(myCanvas draw) {
this.draw = draw;
}
public void run() {
try {
while (true) {
draw.move();
draw.repaint();
Thread.sleep(50);//这里要让它暂停会儿,不然太快没看着就过去了
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class TickerTape extends Frame {// 放画布的窗口
myCanvas drawing = new myCanvas();
Thread t = new Thread(new myThread(drawing));//这里要把你的drawing传过去
TickerTape() {
super("my window");
setLayout(new BorderLayout());
setSize(400, 400);
add("Center", drawing);
t.start();
setVisible(true);
}
public static void main(String[] args) {//这里是String[]啊
new TickerTape();
}
}
2011-11-20
展开全部
public static void main(String [ ]args)漏了中括号了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询