摇摆:JScrollPane导致渲染问题 [英] Swing: JScrollPane causing render issues
问题描述
有时候(并非总是如此),当滚动我的JScrollPane的栏时,某些组件(通常是诸如JLabels之类的文本组件)无法正确地重新绘制,并且最终只能被部分渲染.
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.
我不知道为什么会这样.我尝试在AdjustmentListener中调用paint(),但这似乎无济于事.
I don't know why this is. I've tried invoking paint() inside of an AdjustmentListener, but that doesn't seem to help.
有什么想法吗?
组件的初始化
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.
-
验证
构造和操作了Swing GUI对象. html"rel =" nofollow noreferrer>事件分配线程.
Verify that Swing GUI objects are constructed and manipulated only on the event dispatch thread.
确保您尊重不透明属性.特别是,默认情况下JLabel
不是不不透明.
Ensure that you honor the opacity property. In particular, JLabel
is not opaque by default.
"Swing程序应覆盖paintComponent()
而不是覆盖paint()
."-
"Swing programs should override paintComponent()
instead of overriding paint()
."—Painting in AWT and Swing: The Paint Methods.
附录:此相关的示例结合了这些命令,可滚动显示成千上万个没有伪像的JLabel
实例.
Addendum: Incorporating these dicta, this related example scrolls thousands of flashing JLabel
instances without artifact.
这篇关于摇摆:JScrollPane导致渲染问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!