简体   繁体   English

摇摆:JScrollPane导致渲染问题

[英]Swing: JScrollPane causing render issues

Sometimes (not always) when scrolling the bar of my JScrollPane, some of the components (usually the text ones like JLabels) don't repaint properly and they end up only being partially rendered. 有时(并非总是)在滚动JScrollPane的栏时,某些组件(通常是诸如JLabels之类的文本组件)无法正确地重新绘制,最终只能部分渲染。

I don't know why this is. 我不知道为什么会这样。 I've tried invoking paint() inside of an AdjustmentListener, but that doesn't seem to help. 我尝试在AdjustmentListener中调用paint(),但这似乎无济于事。

Any ideas? 有任何想法吗?

EDIT: Initialization of the components 编辑:组件的初始化

    panel = new JPanel();
    ImageIcon img = new ImageIcon("editor.png");
    setIconImage(img.getImage());
    initComponents();

    final JScrollPane pane = new JScrollPane(panel);
    this.setContentPane(pane);
    //pane.setLayout(new ScrollPaneLayout());
    //pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    Dimension dim = panel.getSize();
    dim.height = dim.height - 100;
    pane.setSize(dim);
    this.setSize(dim);
    AdjustmentListener hListener = new AdjustmentListener() {
        @Override
        public void adjustmentValueChanged(AdjustmentEvent e) {
            repaint();
            for(Component c : panel.getComponents())
                c.repaint();
            for(Component c : pane.getComponents())
                c.repaint();
            panel.repaint();
            panel.revalidate();
            pane.repaint();
            pane.revalidate();
        }
    };
    pane.getVerticalScrollBar().addAdjustmentListener(hListener);
    panel.setVisible(true);
    pane.setVisible(true);

Violations of any of these basic principles can cause rendering artifact. 违反这些基本原则中的任何一项都可能导致渲染失真。

Addendum: Incorporating these dicta, this related example scrolls thousands of flashing JLabel instances without artifact. 附录:结合了这些命令,此相关示例滚动了数千个闪烁的JLabel实例,而没有工件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM