如何在固定位置上绘制JPanel? [英] How to draw on JPanel on fixed position?

查看:248
本文介绍了如何在固定位置上绘制JPanel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将JPanel包装在JScrollPane中,我希望矩形总是在同一位置绘制=使用滚动条移动不会影响矩形的可见性。

I have JPanel wrapped in JScrollPane and I want the rectangle to be drawn always on the same position = moving with scrollbars wont affect the visibility of the rectangle.

I试过以下代码:

    public void paintComponent(Graphics g) {
        g.setColor(Color.red);
        g.drawRect(50, (int)getVisibleRect().getY(), 20 , 20);
    }

但它只在整个JPanel的大小发生变化时重新绘制矩形。

but it only repaints the rectangle when size of whole JPanel is changed.

推荐答案

IIRC, JScrollPane 将尽量减少重绘完成的滚动量,因此它不会总是导致您的组件更新。

IIRC, JScrollPane will try to minimise the amount of redrawing done scrolling, so it wont always cause your component to be updated.

标准技术是使用 JLayeredPane 。将 JScrollPane 添加到较低层,并在其上方添加非不透明玻璃面板组件。请参阅Swing中的如何使用分层窗格教程。

The standard technique is to use a JLayeredPane. Add you JScrollPane to a lower layer, and a non-opaque glass panel component above it. See How to Use a Layered Pane in the Swing tutorial.

这篇关于如何在固定位置上绘制JPanel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆