[英]Update Java Applet label using Threads
I have the following Applet class: 我有以下Applet课程:
public class Marquee extends Applet {
Label label1 = new Label("Testing");
public void pushUpdate( String text ) {
System.out.println( "receiving: " + text );
label1.setText( text );
repaint();
}
public void init() {
ScrollText_2 scrollObj = new ScrollText_2( "Applet test" );
scrollObj.setApplet(this);
add( label1 );
scrollObj.run();
}
}
The ScrollText2 class implements Runnable and has a scroll() method. ScrollText2类实现Runnable并具有scroll()方法。 Right now, all the scroll method does is return the String that the object contains.
现在,所有滚动方法所做的就是返回对象包含的String。 The run() method of this class looks like this:
此类的run()方法如下所示:
while(true) {
try {
marquee.pushUpdate( scroll() );
Thread.sleep( 2000 );
}
catch (InterruptedException e) {}
}
The problem is that when I run the applet, if I call the .run() method, then the label on the marquee never displays. 问题是运行小程序时,如果调用.run()方法,则字幕上的标签将永远不会显示。 I have tried repaint(), just label.setText(), updateUI(), and redraw() to try and get the applet to display the updates but it didn't work.
我已经尝试过repaint(),只是label.setText(),updateUI()和redraw()来尝试使小程序显示更新,但是没有用。
Any suggestions would be greatly appreciated. 任何建议将不胜感激。
Thank you! 谢谢!
You don't call run()
of a Thread or Runnable. 您不调用Thread或Runnable的
run()
。 You call start()
on the Thread or of a Thread that contains the Runnable. 您在线程或包含Runnable的线程上调用
start()
。 Also, you will need to take care to update the GUI components on the GUI's thread. 另外,您将需要注意在GUI线程上更新GUI组件。 For Swing that means using
SwingUtilities.invokeLater(someRunnable)
, and I suspect it can be done similarly with AWT. 对于Swing来说,这意味着使用
SwingUtilities.invokeLater(someRunnable)
,我怀疑可以用AWT来完成。
By the way, why use AWT and not Swing? 顺便说一句,为什么要使用AWT而不是Swing?
You are not creating a new Thread
to run scrollObj
. 您没有创建新的
Thread
来运行scrollObj
。 When you call scrollObj.run()
in Marquee.init()
, scrollObj
hijacks your applet thread. 当您在
Marquee.init()
调用scrollObj.run()
时, scrollObj
劫持您的applet线程。 This means that your main Marquee
update loop, which includes paint()
, is never reached. 这意味着永远不会到达您的主要
Marquee
更新循环,其中包括paint()
。 Invoking repaint()
also does not guarantee that paint()
is called. 调用
repaint()
也不保证会调用paint()
。 Thus, your Marquee
is never painted. 因此,您的
Marquee
永远不会画。
Your code works fine when you replace scrollObj.run();
当您替换
scrollObj.run();
时,您的代码工作正常scrollObj.run();
with 同
new Thread(scrollObj).start();
marquee example in applet for thread like moving something 小程序中的字幕示例,例如移动某些东西的线程
import java.applet.*;
import java.awt.*;
/**<applet code="Marquee" height=768 width=1024></applet>*/
public class Marquee extends Applet implements Runnable {
Color clr[] = { Color.red, Color.green, Color.cyan, Color.blue, Color.orange };
int xx = 0;
int x = 100;
int a = 200;
Thread t = new Thread(this);
public void start() {
t.start();
}
public void paint(Graphics g) {
g.setFont(new Font("Times New Roman", Font.BOLD, 28));
xx++;
if (xx == 3) {
xx = 0;
}
g.setColor(clr[xx]);
g.fillOval(200, 200, x, a);
g.drawString("Vinay", x, 200);
g.drawString("Mayur", a, 300);
x += 1;
a -= 1;
}
public void run() {
try {
System.out.println("sdd");
for (int i = 0; i < 200; i++) {
Thread.sleep(10);
repaint();
}
} catch (Exception e) {
System.out.println("Error:-->" + e);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.