作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个小小程序应该将一个字符串从小程序框架的底部移动到顶部,当它到达顶部时,它应该再次从底部开始。问题是只有当我调整小程序窗口大小时它才会移动。它本身不会移动,为什么会这样呢?
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/
我是一名优秀的程序员,十分优秀!