在java8中重绘()和双缓冲,bug?

我一直在使用Java 8上的Swing中的动画,并且遇到了一些奇怪的行为:在某些其他不相关的组件上调用repaint()后,组件的内容有时会突然变得陈旧。 下面是代码,它为我重现了这种行为:

 import javax.swing.*; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseAdapter; public class Crosshair extends JPanel { private int currentMouseX = 0; private int currentMouseY = 0; public Crosshair() { addMouseMotionListener(new MouseAdapter() { @Override public void mouseMoved(MouseEvent e) { currentMouseX = e.getX(); currentMouseY = e.getY(); repaint(); } }); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.red); g.drawLine(currentMouseX, 0, currentMouseX, getHeight() - 1); g.drawLine(0, currentMouseY, getWidth() - 1, currentMouseY); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame f = new JFrame("Test frame"); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Box content = Box.createHorizontalBox(); content.add(new Crosshair()); content.add(new JButton("Just filler between")); content.add(new Crosshair()); f.getContentPane().add(content); f.setSize(new Dimension(500, 200)); //Bug goes away if double buffering is switched off //RepaintManager.currentManager(f).setDoubleBufferingEnabled(false); f.setVisible(true); }); } } 

重现步骤:

  1. 运行此命令,查看十字光标如何跟随光标。
  2. 通过拖动右侧窗口边框调整窗口大小
  3. 将鼠标hover在右十字准线上,看它是如何跟随光标的
  4. 将鼠标hover在左十字准线上并注意右侧十字准线。
    • 预期:右边的一个将显示与光标离开其区域时相同状态的十字准线。
    • 实际:在某些情况下,它会在窗口停止resize时显示十字准线的状态。

你能重现这种行为吗? 这是一个错误或代码有什么问题吗?

顺便说一句,我使用的是Java 8 update 45(x64),操作系统是Windows 8。