[英]Java Applet not updating in Eclipse's built in applet viewer
I'm just learning about java applets. 我只是在学习Java小程序。 I drew a line on the applet with drawLine() and when I pressed run, it compiled normally and displayed the applet with Eclipse's built in applet viewer.
我使用drawLine()在applet上画了一条线,当我按下run时,它正常编译并使用Eclipse的内置applet viewer显示了applet。 Here's the code
这是代码
import java.applet.*;
import java.awt.*;
public class Lab04b {
public void paint(Graphics g){
g.drawLine(0, 0, 200, 200);
}
}
However, when I commented out the drawLine() and recompiled it and ran it, it displayed the applet with a line on it as if it didn't update when the code changed. 但是,当我注释掉drawLine()并重新编译并运行它时,它在小程序上显示了一行,就好像代码更改时它没有更新一样。 Here's the commented out version:
这是注释掉的版本:
import java.applet.*;
import java.awt.*;
public class Lab04b {
public void paint(Graphics g){
//g.drawLine(0, 0, 200, 200);
}
}
I have tried reopening Eclipse but it still shows the applet with a line on it just like the first time it ran. 我已经尝试过重新打开Eclipse,但是它仍然在小程序上显示一行,就像它第一次运行一样。 Please tell me how I can get Eclipse to update the applet in the built in applet viewer.
请告诉我如何使Eclipse在内置的applet查看器中更新applet。
Your Lab04b class is not an Applet: 您的Lab04b类不是Applet:
import java.applet.*;
import java.awt.*;
public class Lab04b {
public void paint(Graphics g){
g.drawLine(0, 0, 200, 200);
}
}
Since an applet class must extend either Applet or JApplet, and yours does neither. 由于applet类必须扩展Applet或JApplet,而您的两者都不做。 I suggest:
我建议:
paintComponent
method of a JPanel that is displayed within the applet. paintComponent
方法内进行绘制。 init()
method where it will hold its initialization code. init()
方法,该方法将保存其初始化代码。 For example: 例如:
import java.awt.Graphics;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
// an applet class must extend either Applet or JApplet
public class AppletTest extends JApplet {
// it should have an init() method where it holds its initialization code.
@Override
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
add(new DrawingPanel());
}
});
} catch (InvocationTargetException | InterruptedException e) {
e.printStackTrace();
}
}
}
// avoid drawing directly within the applet itself
// but instead draw within a JPanel that is added to the applet
class DrawingPanel extends JPanel {
// this is the method to draw in
@Override
protected void paintComponent(Graphics g) {
// don't forget to call the super method to do "housekeeping" drawing
super.paintComponent(g);
g.drawLine(0, 0, 200, 200);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.