java - 我们如何在 Swing 中的两个面板之间画一条线

标签 java swing user-interface graphics jtable

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

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

enter image description here

在这里我想画出我用粉红色圆圈突出显示的线条。

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

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

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

最佳答案

您可以使用 JFrame/JDialog GlassPane 作为绘画区域轻松做到这一点。只需将您的自定义组件设置为框架的玻璃 Pane ,然后直接在其上绘制链接。

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

这是一个如何在玻璃 Pane 组件上绘制此类“链接”的小示例:

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 http://stackoverflow.com/a/12389479/909085
 */

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 ) );

关于java - 我们如何在 Swing 中的两个面板之间画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12382184/

相关文章:

java - 如何在 jME3 中修改 Swing 对象?

java - JButton 不在 GUI 中显示图标图像

java - 从列中获取数据,然后选择数据所属的整行

c# - 如何在单击游戏对象时创建菜单? (统一)

C - 我可以使用 scanf 暂停等待用户按 Enter 的循环吗?

java - 在 jitpack.io 中找不到 Artifact ,pom 中的几乎所有依赖项都已损坏

java - 无法从 Class 对象获取 Type 标记?

Java Swing 按钮卡住程序

java - java中如何动态显示流程图?

java - RichFaces 4 webapp 的引用架构