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

发布于 2024-12-17 04:57:53 字数 2767 浏览 0 评论 0原文

我在这里完全不知所措。我有一个方法,它遍历包含自定义 FileNode 类的 TreePaths 向量,并提取每个最后一个节点中包含的文件。结果是向量中所有文件名的打印列表。我有一个 for 循环来递增它,它工作得很好,但我将其更改为 while 循环以允许我拥有多个页面。现在,由于某种原因,它只打印最后 11 个文件。

我已经弄清楚是什么原因造成的,但我不知道为什么或如何解决它。我添加了要打印到控制台的“lineCount”和“printPaths.size()”行,发现 lineCount 一直在递增,直到达到 55,然后恢复到 15 并继续递增。名称实际上并没有开始打印,直到它恢复到 15。

我已经在调试模式下单步执行它,它执行了打印循环 55 次,将代码返回到打印函数,告诉它它是打印页面的一部分,然后它会进入一个我没有源代码的打印方法,当我退出该方法时,它又回到了循环的开头,并且 lineCount 为 14。真正奇怪的部分是当它打印出来11 个文件,尽管根据程序,它甚至还没有查看 Vector 的那部分。

如果有人知道造成这种情况的原因,我将非常感谢您的帮助。这是处理打印列表的代码块。希望这已经足够了。

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

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. 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. Now, for some reason, it only prints the last 11 files.

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. The names did not actually start printing until it reverted to 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各空 2024-12-24 04:57:53

您似乎正在从集合 printPaths 中删除,同时向前迭代它。如果是这样,我对事情可能会变得混乱并不感到惊讶。考虑使用迭代器代替。您似乎还在 Paint 或 PaintComponent 方法内执行程序的此逻辑部分,这是一个很大的禁忌。在该方法之外执行逻辑,因为您无法完全控制该方法何时或是否会触发。

You appear to be removing from the collection printPaths while iterating through it in a forward direction. 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. Do your logic outside of this method as you do not have full control over when or even if the method will fire.

惯饮孤独 2024-12-24 04:57:53

我最终发现 Java 本身就可以很好地实现分页,而不需要我尝试做的事情。

I eventually discovered that Java implements pagination pretty well on its own, without needing the stuff I was trying to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文