我一直在尝试使用paint()以外的方法使用Graphics类的drawString()函数进行打印。我已经尝试过该程序,这是对先前疑问的解决方案,但是此代码无法正常工作。请找出我的缺点。谢谢。
下面是它:

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

public class PaintIssue extends Applet {

    Graphics gg; //global Graphics object

    @Override
    public void init() {}

    @Override
    public void paint(Graphics g) {
        g.drawString("Output of paint method",20,20);
        myMethod(); //calling myMethod
    }

    public static void myMethod() {
        gg.drawString("Output of myMethod",20,40);
    }
}

最佳答案

AWT没有“全局图形对象”的概念。您必须传递paint方法收到的Graphics对象。

    @Override
    public void paint(Graphics g) {
        g.drawString("Output of paint method",20,20);
        myMethod(g); //calling myMethod
    }

    public static void myMethod(Graphics g) {
        g.drawString("Output of myMethod",20,40);
    }

关于java - 如何在applet外部paint()方法中进行打印,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35535694/

10-12 00:32
查看更多