【问题标题】:Scrollpane/Viewport swing component showing an overview显示概览的滚动窗格/视口摆动组件
【发布时间】:2012-04-23 13:58:49
【问题描述】:

我正在寻找一个 Swing 组件,它是滚动窗格/视口的扩展,允许打开和导航到当前显示的内容的概览。 我确信在过去的几个月(几年?)中,我看到一些博客文章显示了这样的组件,但再也找不到该组件了。

这是我正在寻找的图片:

如果您记得这样的帖子,或者您的书签中有一些链接,我会感谢您分享信息。

感谢您的帮助。

【问题讨论】:

标签: java swing custom-component


【解决方案1】:

可以和JComponent(s)

1) GlassPane

2) JLayer (Java7) 基于 JXLayer (Java6)

3)JViewport

4)Translucent JDialog的定位

【讨论】:

  • 谢谢,但我想避免重新发明轮子,因为我确信我已经看到其他人已经完成并发布了这样的现有贡献。
  • 抱歉不知道,从来不需要,如果我想这样做,那么通过使用 GlassPane 可以每隔几个小时完成这项工作,请注意我的点默认阻止 MouseEvents,但键盘事件只能采用模态 JDialog ...
【解决方案2】:

这是一个工作示例,但您将需要 java-image-scaling 库(其 LGPL) - 它用于平滑缩放预览图像,否则您将完全毁坏图像:

public static void main ( String[] args )
{
    final JFrame mainFrame = new JFrame ();

    JPanel content = new JPanel ( new GridLayout ( 20, 20 ) );
    for ( int i = 0; i < 20; i++ )
    {
        for ( int j = 0; j < 20; j++ )
        {
            content.add ( new JLabel ( "Test " + i + ":" + j )
            {
                {
                    setBorder ( BorderFactory.createEmptyBorder ( 20, 20, 20, 20 ) );
                }
            } );
        }
    }

    final JScrollPane pane = new JScrollPane ( content );
    pane.setCorner ( JScrollPane.LOWER_TRAILING_CORNER, new JButton ()
    {
        {
            final JButton button = this;
            addActionListener ( new ActionListener ()
            {
                public void actionPerformed ( ActionEvent e )
                {
                    JComponent comp = ( JComponent ) pane.getViewport ().getView ();
                    Dimension size = comp.getSize ();
                    Rectangle viewRect = comp.getVisibleRect ();

                    // Drawing preview
                    BufferedImage image = new BufferedImage ( size.width, size.height,
                            BufferedImage.TYPE_INT_RGB );
                    Graphics2D g2d = image.createGraphics ();
                    comp.print ( g2d );
                    g2d.dispose ();

                    // Rescaling preview
                    int width = 200;
                    int height = comp.getHeight () * width / comp.getHeight ();
                    BufferedImage rescaled =
                            new ResampleOp ( width, height ).filter ( image, null );

                    // Drawing view rect
                    float diff = (float)width / size.width;
                    g2d = rescaled.createGraphics ();
                    g2d.setPaint ( Color.RED );
                    g2d.drawRect ( Math.round ( viewRect.x * diff ),
                            Math.round ( viewRect.y * diff ),
                            Math.round ( viewRect.width * diff ),
                            Math.round ( viewRect.height * diff ) );
                    g2d.dispose ();

                    // Displaying preview
                    final JDialog preview = new JDialog ( mainFrame );
                    preview.setUndecorated ( true );
                    preview.add ( new JLabel ( new ImageIcon ( rescaled ) )
                    {
                        {
                            setBorder ( BorderFactory.createLineBorder ( Color.BLACK ) );
                            setFocusable ( true );
                        }
                    } );
                    Point los = button.getLocationOnScreen ();
                    preview.setSize ( width + 2, height + 2 );
                    preview.setLocation ( los.x - width - 2, los.y - height - 2 );
                    preview.setVisible ( true );

                    preview.requestFocus ();
                    preview.addFocusListener ( new FocusAdapter ()
                    {
                        public void focusLost ( FocusEvent e )
                        {
                            preview.dispose ();
                        }
                    } );
                }
            } );
        }
    } );
    mainFrame.add ( pane );

    mainFrame.setSize ( 600, 600 );
    mainFrame.setLocationRelativeTo ( null );
    mainFrame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    mainFrame.setVisible ( true );
}

所以基本上我创建了面板的快照,重新调整它并放入在按钮附近打开并在焦点丢失时关闭的小对话框中。虽然创建快照可能会浪费时间,但我不知道有什么更好的方法来创建整个滚动窗格容器的渲染预览 - 你必须将它绘制到某些东西上才能显示预览,这意味着它将花费相同的时间。

您还可以轻松修改预览组件,以便您可以通过在预览对话框上拖动红色矩形来移动可见矩形:)

【讨论】:

  • 嗨,这是一个很好的起点,但我仍然会尝试找到我正在考虑的组件。
  • 我记得我在 SwingX 示例中看到过类似的东西——你可以在那里查看。不过不确定...
【解决方案3】:
猜你喜欢
  • 2011-07-29
  • 1970-01-01
  • 2013-03-02
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 1970-01-01
相关资源
最近更新 更多