gpt4 book ai didi

java - 在悬停/单击 JMenuItem 时重新绘制 JPanel

转载 作者:行者123 更新时间:2023-11-29 05:03:49 25 4
gpt4 key购买 nike

我的桌面 Java 应用程序有一个带有多个 JMenuitem 的 JMenubar,它下面是一个 JPanel,当单击下拉菜单中的项目时,我会重新呈现它。

一切正常,但是当我悬停或单击我的 JMenuitems 时,我的 JPanel 正在重新呈现(正在调用我覆盖的 paintComponent)。

这是一个问题,因为在 JPanel 上是以编程方式构建的图像(随机播种),并且构建需要一段时间,所以如果我将鼠标悬停在菜单上太多,我的程序就会挂起..

为什么会这样,我该如何解决?

编辑:即使我对随机值进行播种并获得相同的图像,程序也会进行太多不必要的计算并且速度会变慢。

最佳答案

… (my overridden paintComponent is being called) when I hover or click on my JMenuitems. That is a problem, … Why is this …

这是预期的行为。该工具包会在确定有必要时重新绘制面板:E.G.s

  • 出现或消失的菜单
  • 另一个窗口或对话框在它上面消失/出现
  • 用户调整窗口大小......

… on the JPanel are programmatically constructed images (randomly seeded), and the construction takes a while, …

为避免必须重新创建复杂的绘制,将细节绘制到 BufferedImage 中,然后在绘制方法中绘制图像,或(更简单)将其显示在标签中。

关于java - 在悬停/单击 JMenuItem 时重新绘制 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31091977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com