[英]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. 违反这些基本原则中的任何一项都可能导致渲染失真。
Verify that Swing GUI objects are constructed and manipulated only on the event dispatch thread . 验证仅在事件分发线程上构造和操作了Swing GUI对象。
Ensure that you honor the opacity property. 确保您遵守不透明度属性。 In particular, JLabel
is not opaque by default. 特别是,默认情况下, JLabel
不是不透明的。
"Swing programs should override paintComponent()
instead of overriding paint()
."— Painting in AWT and Swing: The Paint Methods . “ Swing程序应该重写paintComponent()
而不是重写paint()
。” — 在AWT和Swing中绘画:绘画方法 。
Addendum: Incorporating these dicta, this related example scrolls thousands of flashing JLabel
instances without artifact. 附录:结合了这些命令,此相关示例滚动了数千个闪烁的JLabel
实例,而没有工件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.