简体   繁体   English

jscrollpane具有尾随图像

[英]jscrollpane has trailing image

在此输入图像描述

I have a JPanel inside the JScrollPane and it does that whenever I try to scroll. 我在JScrollPane中有一个JPanel,并且每当我尝试滚动时都这样做。 Please help! 请帮忙! How do I fix this? 我该如何解决?

EDIT 编辑

JScrollPane pane;
....
pane = new JScrollPane(GC.createGraph());
pane.setPreferredSize(new Dimension(480,480*2/3));

Placing as an answer for others to see. 放置作为其他人看到的答案。 If you don't call the super.paintComponent, you'll get those artifacts. 如果不调用super.paintComponent,则会得到这些工件。 eg, 例如,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.*;

public class ScrollPaneArtifacts extends JPanel {
   private static final int SPA_WIDTH = 600;
   private static final int SPA_HEIGHT = SPA_WIDTH;

   @Override
   protected void paintComponent(Graphics g) {
      //super.paintComponent(g);
      g.setColor(Color.red);
      g.drawLine(0, 0, getWidth(), getHeight());
      g.drawLine(getWidth(), 0, 0, getHeight());
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(SPA_WIDTH, SPA_HEIGHT);
   }

   private static void createAndShowUI() {
      JScrollPane scrollpane = new JScrollPane(new ScrollPaneArtifacts());
      scrollpane.getViewport().setPreferredSize(new Dimension(400, 400));
      JFrame frame = new JFrame("ScrollPaneArtifacts");
      frame.getContentPane().add(scrollpane);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

You do not need to call super.paintComponent you can simply clear the area to ensure no artifacts are left on the panel from the previous render (which calling super.paintComponent will do). 您无需调用super.paintComponent即可,只需清除该区域即可确保以前的渲染中面板上没有残影(调用super.paintComponent即可)。

@Override
protected void paintComponent(Graphics g) {
  g.clearRect(0,0,getWidth(),getHeight());
  g.setColor(Color.red);
  g.drawLine(0, 0, getWidth(), getHeight());
  g.drawLine(getWidth(), 0, 0, getHeight());
}

Try this in Hovercrafts code if you like. 如果愿意,请在Hovercrafts代码中尝试此操作。

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

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