简体   繁体   English

Java Applet未在Eclipse的内置Applet Viewer中更新

[英]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: 我建议:

  • Have the class extend JApplet 让类扩展JApplet
  • But don't draw directly within it. 但是不要直接在其中绘制。
  • Instead draw within the paintComponent method of a JPanel that is displayed within the applet. 而是在显示在小程序内的JPanel的paintComponent方法内进行绘制。
  • Be sure to give your applet class an init() method where it will hold its initialization code. 确保为applet类提供一个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);
    }
}
  • Having said this, consider not learning about applets since they're rarely used now-a-days. 话虽如此,请考虑不要学习小程序,因为它们如今已很少使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM