我已经扩展JPanel并覆盖了该paint方法,以便JButton在面板中某些 s 的位置之间绘制一些额外的线。但是,只有在 gui 最大化时才能正确绘制线条,否则它们会以完全错误的偏移量绘制。
绘制我正在使用的线('rootNode'和'child'都是JButton,g是Graphics覆盖paint方法的参数):
Point sourcePoint = new Point(rootNode.getLocation());
Point destPoint = new Point(child.getLocation());
SwingUtilities.convertPointToScreen(sourcePoint, rootNode.getParent());
SwingUtilities.convertPointToScreen(destPoint, child.getParent());
g.drawLine(sourcePoint.x, sourcePoint.y, destPoint.x, destPoint.y);
未最大化时不正确的线条图片:http ://postimage.org/image/ws0yo9chf/ 最大化时正确的图片:http: //postimage.org/image/fq84m5xmb/