大家好!
我需要一点帮助,因为我无法真正弄清楚我做错了什么。我正在开发一个写信封的程序。该程序有4个JTextField。 姓名、城市、地址、邮政编码。我在 Name JTextField 上使用 keylistener 来识别 Enter 按钮,并添加一个新的 Name JTextField。 (基本上,它设置第二个名称 JTextField 可见)。因此,总共您可以有 4 个名称 JTextField 和其他(城市、地址、邮政编码)总共 7 个。还有打印 jbutton、用于查看已写好等待打印的信封的 jtable 等。当我使用 g.drawString() 时,我使用预定义的 x,y 值,该值根据 Font Metrics 的 StringWidth() 方法测量的“名称”字段的文本长度动态变化,并使用右侧边距值计算......无论如何。我的主要问题在这里:
if (name.length() > address.length()) {
g.drawString(name,
(250 - nameCord_X) + 46, 214); //this is working
g.drawString(city, (250 - nameCord_X) + 46, 226);
g.drawString(address, (250 - nameCord_X) + 46, 238);
g.drawString(postal, (250 - nameCord_X + 46, 250);
} else {
if (address.length() > name.length()
&& name2.isEmpty())
{ // this is working
g.drawString(name, (250 - addressCord_X) + 46, 214);
g.drawString(city, (250 - addressCord_X) + 46, 226);
g.drawString(address, (250 - addressCord_X) + 46, 238);
g.drawString(postal, (250 - addressCord_X + 46, 250);
}
}
if (name2.length() > name.length()
&& name2.length() > address.length()
&& name2.isVisible())
{// this is working
g.drawString(name, (250 - name2Cord_X) + 46, 202);
g.drawString(name2, (250 - name2Cord_X) + 46, 214);
g.drawString(city, (250 - name2Cord_X) + 46, 226);
g.drawString(address, (250 - name2Cord_X) + 46, 238);
g.drawString(postal, (250 - name2Cord_X) + 46, 250);
}
//This is the part that doesnt working. it prints out 5 lines
// but the 2nd line is the same as first line,
// and the real 2nd line value is printed over.
// For example Name: Lion Name2:Lion (altough I typed for
// example horse into name2 field, and the program draw
// the horse string over the 2nd line which contains Lion.)
//Altough it prints out the all 5 row, it messing only with
//the second row :-/
if( (name.length() > name2.length()
&& name.length() > address.length()
&& name2.isVisible()))
{
g.drawString(name, (250 - nameCord_X) + 46, 202);
g.drawString(name2, (250 - nameCord_X) + 46, 214);
g.drawString(city, (250 - nameCord_X) + 46, 226);
g.drawString(address, (250 - nameCord_X) + 46, 238);
g.drawString(postal, (250 - nameCord_X) + 46, 250);
}
最佳答案
那里有一些复杂的 if
block 。此外,还不清楚变量的类型是什么。看起来它们是 String
但例如您对 name2
调用 isVisible()
。
据我了解,您正在使用 nameCord_X
、addressCord_X
、name2Cord_X
进行对齐。您希望所有字符串都左对齐,并从该点开始绘图,这样最大的字符串就可以正确放置,这样它就不会超出信封的右侧。这是正确的吗?
如果是,那么我建议你首先找到上述长度中的最大值。只有当每一项具有有意义的值(value)时,您才应该考虑它。然后您可以执行以下操作:
int max = ... // maximum of the above values - only not empty values should be taken into account
int dy = 12; // it seems you always leave 12 pixels between the lines
int y = ...; // the value with the topmost y. It seems you use 202 when both names are present 214 when name2 is not given
int x = 250 - max + 46;
String[] labels = new String[] {name, name2, city, address, postal};
for (String label : labels) {
if (label == null || label.length() == 0) {
continue; // ignore null or empty values
}
g.drawString(name, x, y);
y += dy;
}
希望对你有帮助
关于java - 在信封上绘制文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17863325/