我擴展了JPanel
並覆蓋了paint
方法,以便在面板中某些JButton
s的位置之間畫出一些額外的線條。但是,只有當gui最大化時纔會正確繪製線條,否則它們將被完全繪製爲錯誤的偏移量。當線條未被最大化時,線條被塗在錯誤的位置
畫我使用的線(「根節點」和「孩子」都是JButton
,g
是重寫paint
方法的Graphics
參數):
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/
上線的位置的效果我們展示的圖片 –
你應該重寫'的paintComponent (..)'''JComponent'並在那裏繪畫,而不是'paint(..)'方法。並且不要忘記所有重要的'super.paintComponent(g);'作爲覆蓋'paintComponent()'方法的第一行。 –
覆蓋paintComponent結果的效果相同。我現在要上傳圖片.. – user973758