简体   繁体   中英

JAVA: Can't find symbol: method paint

Here's my code:

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

public class Face extends Applet {
    public void paint(Graphics frame) {

        // bits of code here
    }

    public static void main(String[] args) {
        String printedOut;
        printedOut = printedOut.paint();
        System.out.println(printedOut);
    }
}

I've looked for different solutions to this, but so far, my search has yielded no results. I ran the code two months ago in school and it worked fine, but now any code I write using the paint method, it just doesn't recognize it. I'm using Netbeans, any solution is greatly appreciated. (I'm trying to make a face.) Also, I know my method of printing it out to the applet is pretty remedial but it works, I'd love to see a different way of doing that, thanks in advance.

This may be causing your error:

String printedOut;
printedOut = printedOut.paint();

String does not have a paint() method. I'd give a recommendation on how to fix this, but I'm not sure what you're trying to accomplish with that line, so I simply recommend deleting the offending line. If you were thinking of creating a Face object within the main method, and then calling its paint(...) method directly, don't. You will almost never want to call paint directly (unless you're trying to draw the applet to a BufferedImage).

Also as noted in my comments,

  • Applets shouldn't even have main methods as they won't be called.
  • In future posts, please post the full error message.
  • AWT is a dead technology and is at least 2 major generations behind (Swing and now JavaFX).
  • Applets are a dead technology and have largely been superseded by HTML5/CSS3/JavaScript.

Also, regarding your questions at the bottom:

I've looked for different solutions to this, but so far, my search has yielded no results.

You can avoid posting this as it gives us no insight into your actual problem.

I ran the code two months ago in school and it worked fine,

But it wasn't the code that you posted, as there's no way that it would work fine.

but now any code I write using the paint method, it just doesn't recognize it.

I'm not sure what to make of this. If you mean that it doesn't recognize you're calling paint() on a String object, then that makes sense that it doesn't recognize it, since String doesn't have this method. Your compiler however is recognizing your public void paint(Graphics g) method.

I'm using Netbeans, any solution is greatly appreciated. (I'm trying to make a face.)

The solution is not use non-compilable code. As for your making a face, that's a completely different issue, and you'll want to post code inside of your paint method that uses the Graphics parameter to achieve this.

Also, I know my method of printing it out to the applet is pretty remedial but it works, I'd love to see a different way of doing that.

Not sure what you're asking here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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