如何在JPanel上固定位置绘图?

发布于 2024-09-01 18:04:04 字数 296 浏览 3 评论 0原文

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

我尝试了以下代码:

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

但它仅在整个 JPanel 的大小更改时重新绘制矩形。

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 tried following code:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酷遇一生 2024-09-08 18:04:04

IIRC,JScrollPane 会尝试最小化滚动时的重绘量,因此它不会总是导致组件更新。

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

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

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.

じее 2024-09-08 18:04:04

也许是这样的:

import java.awt.*;
import javax.swing.*;

public class ScrollPanePaint extends JFrame
{
    public ScrollPanePaint()
    {
        JPanel panel = new JPanel();
        panel.setOpaque( false );
        panel.setPreferredSize( new Dimension(400, 400) );

        JViewport viewport = new JViewport()
        {
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                g.setColor( Color.BLUE );
                g.drawArc( 100, 100, 80, 80, 0, 360);
            }
        };

        viewport.setView( panel );
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewport( viewport );
        scrollPane.setPreferredSize( new Dimension(300, 300) );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        JFrame frame = new ScrollPanePaint();
        frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
     }
}

Maybe something like this:

import java.awt.*;
import javax.swing.*;

public class ScrollPanePaint extends JFrame
{
    public ScrollPanePaint()
    {
        JPanel panel = new JPanel();
        panel.setOpaque( false );
        panel.setPreferredSize( new Dimension(400, 400) );

        JViewport viewport = new JViewport()
        {
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                g.setColor( Color.BLUE );
                g.drawArc( 100, 100, 80, 80, 0, 360);
            }
        };

        viewport.setView( panel );
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewport( viewport );
        scrollPane.setPreferredSize( new Dimension(300, 300) );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        JFrame frame = new ScrollPanePaint();
        frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
     }
}
浪漫人生路 2024-09-08 18:04:04

尝试 setLocation 方法。访问 setLocation 了解更多信息。

Try setLocation method. Visit setLocation for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文