gpt4 book ai didi

java - 可运行的 Java 小程序仅在调整大小时才起作用

转载 作者:行者123 更新时间:2023-12-01 13:33:23 25 4
gpt4 key购买 nike

这个小小程序应该将一个字符串从小程序框架的底部移动到顶部,当它到达顶部时,它应该再次从底部开始。问题是只有当我调整小程序窗口大小时它才会移动。它本身不会移动,为什么会这样呢?

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Zad1 extends Applet implements Runnable {

Thread runner;
int yPos = 500;

public void start() {
if (runner == null) {
runner = new Thread(this);
}
}

public void stop() {
if (runner != null) {
runner = null;
}
}

public void run() {
while (true) {
repaint();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

}

public void paint(Graphics g) {
g.drawString("Hello java", 50, yPos);
yPos--;
if (yPos < -30)
yPos = 500;
}
}

最佳答案

线程未启动

        runner = new Thread(this);
runner.start(); // <----------- Insert this!

但请注意,这个小程序的风格在很多方面都很糟糕(例如,“paint”中不应该有逻辑,您可能根本不应该覆盖 Applet 的“paint”,您应该考虑 JApplet 等。 ..)。您可能应该阅读http://docs.oracle.com/javase/tutorial/uiswing/components/applet.html和其他例子。

关于java - 可运行的 Java 小程序仅在调整大小时才起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21430187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com