如何在Swing中绘制两个面板之间的线?

6

我只想通过画一条线来连接面板。

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

enter image description here

这里我想画出像我用粉色圆圈标记的那样的线。

这是我正在使用的创建jtables的代码片段

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

这里的FieldMapperJTable是我定制的jtable类。

任何形式的帮助或使用其他方法的想法都将不胜感激。非常感谢您的帮助。 - Java_Alert
1
@Stanislav非常感谢您的帖子。这对我很有用。"http://java-sl.com/connector.html" - Java_Alert
@Will,不知道你为什么删除了Stani的回答——它绝对是有帮助的。 - kleopatra
1
@kleopatra 那篇帖子真的很好,让人获得了很多知识。 - Java_Alert
1
重复链接:JConnector项目 由 @Stanislav。 - kleopatra
1个回答

8
您可以使用JFrame/JDialog GlassPane作为绘画区域轻松实现这一点。只需将您的自定义组件设置为框架的玻璃板,并直接在其上绘制链接即可。
您也可以使用框架/对话框的分层面板来完成相同的操作。
以下是一个小型工作示例,演示如何在玻璃板组件上绘制此类“链接”:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

/**
 * @see https://dev59.com/4WjWa4cB1Zd3GeqPpli5#12389479
 */

public class ComponentLinkerTest extends JComponent
{
    private Map<JComponent, JComponent> linked;

    public ComponentLinkerTest ()
    {
        super ();
        linked = new HashMap<JComponent, JComponent> ();
    }

    public void link ( JComponent c1, JComponent c2 )
    {
        linked.put ( c1, c2 );
        repaint ();
    }

    protected void paintComponent ( Graphics g )
    {
        Graphics2D g2d = ( Graphics2D ) g;
        g2d.setRenderingHint ( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );

        g2d.setPaint ( Color.BLACK );
        for ( JComponent c1 : linked.keySet () )
        {
            Point p1 = getRectCenter ( getBoundsInWindow ( c1 ) );
            Point p2 = getRectCenter ( getBoundsInWindow ( linked.get ( c1 ) ) );
            g2d.drawLine ( p1.x, p1.y, p2.x, p2.y );
        }
    }

    private Point getRectCenter ( Rectangle rect )
    {
        return new Point ( rect.x + rect.width / 2, rect.y + rect.height / 2 );
    }

    private Rectangle getBoundsInWindow ( Component component )
    {
        return getRelativeBounds ( component, getRootPaneAncestor ( component ) );
    }

    private Rectangle getRelativeBounds ( Component component, Component relativeTo )
    {
        return new Rectangle ( getRelativeLocation ( component, relativeTo ),
                component.getSize () );
    }

    private Point getRelativeLocation ( Component component, Component relativeTo )
    {
        Point los = component.getLocationOnScreen ();
        Point rt = relativeTo.getLocationOnScreen ();
        return new Point ( los.x - rt.x, los.y - rt.y );
    }

    private JRootPane getRootPaneAncestor ( Component c )
    {
        for ( Container p = c.getParent (); p != null; p = p.getParent () )
        {
            if ( p instanceof JRootPane )
            {
                return ( JRootPane ) p;
            }
        }
        return null;
    }

    public boolean contains ( int x, int y )
    {
        return false;
    }

    private static ComponentLinkerTest linker;

    public static void main ( String[] args )
    {
        setupLookAndFeel ();

        JFrame frame = new JFrame ();

        linker = new ComponentLinkerTest ();
        frame.setGlassPane ( linker );
        linker.setVisible ( true );

        JPanel content = new JPanel ();
        content.setLayout ( new GridLayout ( 10, 5, 5, 5 ) );
        content.setBorder ( BorderFactory.createEmptyBorder ( 5, 5, 5, 5 ) );
        frame.add ( content );

        for ( int i = 0; i < 50; i++ )
        {
            final JButton button = new JButton ( "Button" + i );
            button.addActionListener ( new ActionListener ()
            {
                public void actionPerformed ( ActionEvent e )
                {
                    link ( button );
                }
            } );
            content.add ( button );
        }

        frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
        frame.pack ();
        frame.setLocationRelativeTo ( null );
        frame.setVisible ( true );
    }

    private static JButton last = null;

    private static void link ( JButton button )
    {
        if ( last == null )
        {
            last = button;
        }
        else
        {
            linker.link ( last, button );
            last = null;
        }
    }

    private static void setupLookAndFeel ()
    {
        try
        {
            UIManager.setLookAndFeel ( UIManager.getSystemLookAndFeelClassName () );
        }
        catch ( ClassNotFoundException e )
        {
            e.printStackTrace ();
        }
        catch ( InstantiationException e )
        {
            e.printStackTrace ();
        }
        catch ( IllegalAccessException e )
        {
            e.printStackTrace ();
        }
        catch ( UnsupportedLookAndFeelException e )
        {
            e.printStackTrace ();
        }
    }
}

结果如下:
(只需依次单击任意两个按钮即可将它们链接在一起)
enter image description here

附:要使线条更粗,可以在绘画时更改描边:

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

我不确定这是否仍然适用,但在 Swing 新鲜出炉的时候,当我尝试使用 glasspane 时,我遇到了很多处理事件的问题。 - Jens Schauder
+1 对于在组件之上进行渲染以及令人赞叹的注释风格。 :-) - trashgod
重复我对已删除答案的评论:@Sunil 如果您正在使用jdk7,您可以考虑获取Stan的逻辑并在 JLayer/UI 中实现它。和类似这样的情况:旧的 GlassPane 难以正确处理(多年来没有变化)。 - kleopatra
对于早期版本,我使用JXLayer,它非常相似且已经非常稳定。至于“没有玻璃窗的问题”-我清楚地记得亚历山大(Alexander)曾经为了使J/X/Layer的玻璃窗真正安全和有用而苦苦挣扎;-)不过,我忘记了细节,如果您感兴趣,可以在java.net上搜索他的博客。 - kleopatra
@kleopatra 嗯,我实际上想探索JDK7中提供的JLayer,看看它是如何工作的,以及可以做什么。还有哪些不能做...像往常一样 :) - Mikle Garin
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接