【问题标题】:How to replace a character within a string with fillPolygon using array如何使用数组用fillPolygon替换字符串中的字符
【发布时间】:2013-02-27 14:07:39
【问题描述】:

我需要在文本框中输入一个单词/句子,然后打印出我输入的内容,但是当我有一个特殊字符时,它会打印出我的 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

【问题讨论】:

  • 请不要在您的问题中包含“提前致谢”。我们的风格专注于问题,而不是寒暄。

标签: java arrays string replace applet


【解决方案1】:

您需要在 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

有一些布局问题需要解决 - 我将把它留作练习:)

【讨论】:

  • 谢谢,我已经修改了代码,现在可以编译了,但是当我在编译器行中输入文本并且出现红色错误消息行时?你有什么想法?谢谢
猜你喜欢
  • 1970-01-01
  • 2014-03-21
  • 2013-11-08
  • 1970-01-01
  • 2015-12-25
  • 2019-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多