簡體   English   中英

阻止JScrollPane重繪JPanel

[英]Stop JScrollPane from redraw JPanel

我有一個JPanel及其周圍的JScrollPane,問題是當我使用JScrollPane時,會調用JPanels重繪方法。 我想禁用它,因為我的JPanel會在適當的時候自行重繪。

我想要它,所以它只更新了paint方法的getClipBounds(),而無需調用paint方法。

您不能這樣做-由於視口顯示所包含的JPanel的不同部分,具體取決於滾動條的位置,實際上必須重新顯示必須重新繪制的區域,並且以前可能沒有繪制過。

由於JScrollPane不知道所包含的Component是如何實現的,以及它是否重繪整個區域還是僅重繪需要重繪的區域,因此它強制所包含的Component在滾動時重繪自身。

但是,您可以將要顯示的內容呈現為位圖,然后在paintComponent(Graphics)方法中繪制位圖。 因此,您可以有效地緩沖繪制的內容,並可以在需要時隨時啟動對緩沖位圖的更新。

為了繪制到位圖上,可以執行以下操作:

BufferedImage buffer; // this is an instance variable

private void updateBuffer(){
   // Assuming this happens in a subclass of JPanel, where you can access
   // getWidth() and getHeight()
   buffer=new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
   Graphics g=buffer.getGraphics();
   // Draw into the graphic context g...
   g.dispose();
}

然后,在您的JPanel中,重寫paintComponent方法:

public void paintComponent(Graphics g){
    g.drawImage(buffer, 0, 0, this);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM