2013-07-25 42 views
0

Hy guys!信封上绘制文字

我需要一点帮助,因为我无法真正弄清楚我做错了什么。我正在制作一个信封写作计划。该程序有4个JTextField。 姓名,城市,地址,邮政编码。我使用Name JTextField上的keylistener来识别Enter按钮,并添加一个新的Name JTextField。 (基本上,它设置第二个名称JTextField可见)。因此,总共可以有4个名称JTextField和其他(城市,地址,邮政编码)共7个。还有打印jbutton,jtable用于查看已经写好的信封等待打印等等。当我使用g.drawString()时,我使用预定义的x,y值,它们根据由Font Metrics的StringWidth()方法测量的Names字段的文本长度动态变化,并使用右侧边距值计算。我的主要问题在这里:

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); 
        } 

回答

1

您在那里有一些复杂的if块。也不清楚变量的类型是什么。看起来它们是String但是例如您致电isVisible()name2

据我所知,您正在使用nameCord_X,addressCord_X,name2Cord_X进行比对。您希望所有字符串保持对齐,并在该位置开始绘图,以便允许最大的字符串正确贴合,以便它不超过信封的右侧。那是对的吗?

如果是,那么我建议你先找到以上长度的最大值。只有当它具有有意义的价值时,你才应该考虑每一个。然后,你可以做到以下几点:

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; 
}  

我希望它能帮助

+0

谢谢您的回答。我现在就试试看。是的,你对任何事情都看得很对,对于缺乏信息感到抱歉。我尝试了你的想法后,我会回到这里:) –