简体   繁体   English

循环中来自Java Graphics drawString调用的奇怪行为

[英]Strange behavior from a Java Graphics drawString call in a loop

I'm at a complete loss here. 我在这里完全不知所措。 I have a method that goes through a Vector of TreePaths containing a custom FileNode class, and extracts the File contained in each last node. 我有一个遍历包含自定义FileNode类的TreePaths向量的方法,并提取每个最后一个节点中包含的File。 The result is a printed list of all the filenames in the Vector. 结果是向量中所有文件名的打印列表。 I had a for loop to increment through it, and it worked fine, but I changed it to a while loop to allow me to have multiple pages. 我有一个for循环来递增它,它工作正常,但我将其更改为while循环,以允许我拥有多个页面。 Now, for some reason, it only prints the last 11 files. 现在,由于某种原因,它仅打印最后11个文件。

I've figured out what's causing it, but I don't know why or how to fix it. 我已经弄清楚是什么原因造成的,但是我不知道为什么或如何解决它。 I added in lines to print to the console 'lineCount' and 'printPaths.size()', and discovered that lineCount was incrementing until it hit 55, then reverting to 15 and continuing to increment. 我添加了几行以打印到控制台'lineCount'和'printPaths.size()',发现lineCount一直递增,直到达到55,然后恢复为15,然后继续递增。 The names did not actually start printing until it reverted to 15. 名称实际上并没有开始打印,直到恢复为15。

I've stepped through it in debug mode, and it goes through the print loop 55 times, returns the code to the print function that tells it it's part of the printed page, then it drops into a print method for which I don't have the source, and when I step back up out of that, it is back at the beginning of the loop, and lineCount is at 14. The really odd part is that's when it prints out the 11 files, even though according to the program it hasn't even looked at that part of the Vector yet. 我已经在调试模式下完成了它,它经历了55次打印循环,将代码返回给打印函数,告诉它它是打印页面的一部分,然后将其放入我不希望使用的打印方法中有源文件,当我退出该源文件时,它又回到循环的开始,而lineCount在14。真正奇怪的是那是它打印出11个文件的时间,即使根据程序它甚至还没有查看Vector的那部分。

If anyone has the slightest idea what's causing that, I would really appreciate the help. 如果有人有什么想法的话,我将非常感谢您的帮助。 Here's the chunk of code that deals with printing the list. 这是处理打印列表的代码块。 Hopefully that's enough. 希望这足够了。

        lineCount = 13;
    int lineSpacing = 14;
    g.setFont(new Font("Dialog", Font.PLAIN, 10));
    boolean color = true;
    if (summ) {
        g.drawString(((TreePath) printPaths.get(0)).getPathComponent(
                ((TreePath) printPaths.get(0)).getPathCount() - 3)
                .toString()
                + " : "
                + ((TreePath) printPaths.get(0)).getPathComponent(
                        ((TreePath) printPaths.get(0)).getPathCount() - 5)
                        .toString(), 36, lineCount * lineSpacing);
        lineCount++;
        //for (int j = 1; j < printPaths.size(); j++) {
        while((printPaths.size()>1) && lineCount<55){
            String type = ((TreePath) printPaths.get(1)).getPathComponent(
                    ((TreePath) printPaths.get(1)).getPathCount() - 5)
                    .toString();
            String date = ((TreePath) printPaths.get(1)).getPathComponent(
                    ((TreePath) printPaths.get(1)).getPathCount() - 3)
                    .toString();
            String typeU = ((TreePath) printPaths.get(0))
                    .getPathComponent(
                            ((TreePath) printPaths.get(1)).getPathCount() - 5)
                    .toString();
            String dateU = ((TreePath) printPaths.get(0))
                    .getPathComponent(
                            ((TreePath) printPaths.get(1)).getPathCount() - 3)
                    .toString();
            if (!(type == typeU) && (date == dateU)) {
                lineCount++;
                g.setColor(c1);
                g.drawString(date + " : " + type, 36, lineCount
                        * lineSpacing);
                lineCount++;
            }
            if(color)
                g.setColor(c1);
            else
                g.setColor(c2);
            g.drawString(((TreePath) printPaths.get(1))
                    .getLastPathComponent().toString(), 54, lineCount
                    * lineSpacing);
            color=!color;
            lineCount++;
            printPaths.remove(0);
            System.out.println(printPaths.size());
            System.out.println(lineCount);
        }
    }

You appear to be removing from the collection printPaths while iterating through it in a forward direction. 您似乎正在从集合printPaths中删除,同时正向遍历它。 If so, I'm not surprised that this might be messed up. 如果是这样,我可能会搞砸了,这并不奇怪。 Consider using an iterator instead. 考虑改用迭代器。 You also appear to be doing this logic portion of your program inside of a paint or paintComponent method which is a big no-no. 您似乎还需要在Paint或paintComponent方法内部执行程序的此逻辑部分,这是很大的禁止。 Do your logic outside of this method as you do not have full control over when or even if the method will fire. 在此方法之外执行逻辑,因为您无法完全控制该方法何时或什至将触发。

最终,我发现Java可以很好地实现分页,而无需我试图做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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