gpt4 book ai didi

java - 显示概览的滚动 Pane /视口(viewport) Swing 组件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:42 26 4
gpt4 key购买 nike

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

这是我正在寻找的图片:

scrollpane with overview

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

感谢您的帮助。

最佳答案

这是一个工作示例,但您需要 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 );
}

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

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

关于java - 显示概览的滚动 Pane /视口(viewport) Swing 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10102965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com