java – 我们如何在两个面板之间画一条线

前端之家收集整理的这篇文章主要介绍了java – 我们如何在两个面板之间画一条线前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
只想通过画线来连接面板.

我有两个面板,两个面板都包含一个Jtable.我想将一个面板的jtable的每个单元连接到另一个面板的另一个Jtable.

在这里,我想绘制像我用粉红色圆圈突出显示的线条.

这是我用来创建jtables的代码片段

  1. DefaultTableModel fcdbDataModel = new DefaultTableModel(fcdbIdTxnArray,fcdbIdTxnColumnArray);
  2. fcdbIdTxnJTable = new FieldMapperJTable(fcdbDataModel);

这里FieldMapperJTable是我自定义的jtable类.

解决方法

您可以使用JFrame / JDialog GlassPane作为绘图字段轻松完成.只需将自定义组件设置为框架的玻璃窗格,然后直接在其上绘制链接.

您也可以使用框架/对话框的分层窗格执行相同操作.

这是一个如何在玻璃窗格组件上绘制这样的“链接”的小工作示例:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7.  
  8. /**
  9. * @see https://stackoverflow.com/a/12389479/909085
  10. */
  11.  
  12. public class ComponentLinkerTest extends JComponent
  13. {
  14. private Map<JComponent,JComponent> linked;
  15.  
  16. public ComponentLinkerTest ()
  17. {
  18. super ();
  19. linked = new HashMap<JComponent,JComponent> ();
  20. }
  21.  
  22. public void link ( JComponent c1,JComponent c2 )
  23. {
  24. linked.put ( c1,c2 );
  25. repaint ();
  26. }
  27.  
  28. protected void paintComponent ( Graphics g )
  29. {
  30. Graphics2D g2d = ( Graphics2D ) g;
  31. g2d.setRenderingHint ( RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON );
  32.  
  33. g2d.setPaint ( Color.BLACK );
  34. for ( JComponent c1 : linked.keySet () )
  35. {
  36. Point p1 = getRectCenter ( getBoundsInWindow ( c1 ) );
  37. Point p2 = getRectCenter ( getBoundsInWindow ( linked.get ( c1 ) ) );
  38. g2d.drawLine ( p1.x,p1.y,p2.x,p2.y );
  39. }
  40. }
  41.  
  42. private Point getRectCenter ( Rectangle rect )
  43. {
  44. return new Point ( rect.x + rect.width / 2,rect.y + rect.height / 2 );
  45. }
  46.  
  47. private Rectangle getBoundsInWindow ( Component component )
  48. {
  49. return getRelativeBounds ( component,getRootPaneAncestor ( component ) );
  50. }
  51.  
  52. private Rectangle getRelativeBounds ( Component component,Component relativeTo )
  53. {
  54. return new Rectangle ( getRelativeLocation ( component,relativeTo ),component.getSize () );
  55. }
  56.  
  57. private Point getRelativeLocation ( Component component,Component relativeTo )
  58. {
  59. Point los = component.getLocationOnScreen ();
  60. Point rt = relativeTo.getLocationOnScreen ();
  61. return new Point ( los.x - rt.x,los.y - rt.y );
  62. }
  63.  
  64. private JRootPane getRootPaneAncestor ( Component c )
  65. {
  66. for ( Container p = c.getParent (); p != null; p = p.getParent () )
  67. {
  68. if ( p instanceof JRootPane )
  69. {
  70. return ( JRootPane ) p;
  71. }
  72. }
  73. return null;
  74. }
  75.  
  76. public boolean contains ( int x,int y )
  77. {
  78. return false;
  79. }
  80.  
  81. private static ComponentLinkerTest linker;
  82.  
  83. public static void main ( String[] args )
  84. {
  85. setupLookAndFeel ();
  86.  
  87. JFrame frame = new JFrame ();
  88.  
  89. linker = new ComponentLinkerTest ();
  90. frame.setGlassPane ( linker );
  91. linker.setVisible ( true );
  92.  
  93. JPanel content = new JPanel ();
  94. content.setLayout ( new GridLayout ( 10,5,5 ) );
  95. content.setBorder ( BorderFactory.createEmptyBorder ( 5,5 ) );
  96. frame.add ( content );
  97.  
  98. for ( int i = 0; i < 50; i++ )
  99. {
  100. final JButton button = new JButton ( "Button" + i );
  101. button.addActionListener ( new ActionListener ()
  102. {
  103. public void actionPerformed ( ActionEvent e )
  104. {
  105. link ( button );
  106. }
  107. } );
  108. content.add ( button );
  109. }
  110.  
  111. frame.setDefaultCloSEOperation ( JFrame.EXIT_ON_CLOSE );
  112. frame.pack ();
  113. frame.setLocationRelativeTo ( null );
  114. frame.setVisible ( true );
  115. }
  116.  
  117. private static JButton last = null;
  118.  
  119. private static void link ( JButton button )
  120. {
  121. if ( last == null )
  122. {
  123. last = button;
  124. }
  125. else
  126. {
  127. linker.link ( last,button );
  128. last = null;
  129. }
  130. }
  131.  
  132. private static void setupLookAndFeel ()
  133. {
  134. try
  135. {
  136. UIManager.setLookAndFeel ( UIManager.getSystemLookAndFeelClassName () );
  137. }
  138. catch ( ClassNotFoundException e )
  139. {
  140. e.printStackTrace ();
  141. }
  142. catch ( InstantiationException e )
  143. {
  144. e.printStackTrace ();
  145. }
  146. catch ( IllegalAccessException e )
  147. {
  148. e.printStackTrace ();
  149. }
  150. catch ( UnsupportedLookAndFeelException e )
  151. {
  152. e.printStackTrace ();
  153. }
  154. }
  155. }

结果如下:
(只需一个接一个地点击任意两个按钮,它们就会被链接)

附:要使线条更粗,您可以在绘画时更改笔划:

  1. g2d.setStroke ( new BasicStroke ( 5f ) );

猜你在找的Java相关文章