我需要在文本框中输入一个单词/句子,然后打印出我输入的内容,但是当我有一个特殊字符时,它会打印出我的 fillPolygon 形状。 我已使用数组尝试使用 If 语句将字符替换为多边形,但是我不断收到错误消息:
[line: 49] Error: method drawH in class Stringpoly cannot be applied to given types; required: java.awt.Graphics,int,int found:
java.awt.Graphics,int[],int[] reason: actual argument int[] cannot be
converted to int by method invocation conversion
最佳答案
您需要在 drawH
方法中使用 int
数组参数来匹配传入的值。替换
public void drawH(Graphics g, int xpoints, int ypoints)
与
public void drawH(Graphics g, int[] xpoints, int[] ypoints)
另一个错误是:
drawString
的参数与该方法的任何可用版本都不匹配:
g.drawString(pr_charArray[i]);
看看docs对于这个方法。你需要类似的东西
g.drawString(new String(pr_charArray), xpos, ypos);
另一个问题是您没有在 init
中实例化或添加 pr_text
TextField
。你可以这样做:
pr_text = new TextField(20);
add(pr_text);
一些旁注:
- Java 使用驼峰命名法,这将使
pr_text
prText
AWT
非常古老。考虑使用轻量级Swing graphics library 。
有一些布局问题需要解决 - 我将把它作为练习:)
关于java - 如何使用数组用 fillPolygon 替换字符串中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15114134/