2010-02-02 79 views
0

我正在使用Java JRE 1.6.7並具有JComponent和JScrollPane。我無法得到雙緩衝工作,這總是導致閃爍。如果我使用了Canvas,則需要緩衝處理,但這會在與JScrollPane結合使用時產生問題。自定義繪製組件不在JScrollPane內繪製

所以我下載了JRE 1.6.18,希望能解決其中的一個問題。那麼現在JScrollPane中的JComponent根本就沒有正確繪製。它僅繪製JComponent的外部區域,就像JScrollPane在邊界之外繪製它一樣。

下面是代碼未drawing..this中的區域的1個像素寬的白色輪廓的結果應該在哪裏發生拉絲的例子:

public void paint(Graphics arg0) { 



Graphics2D graphics = (Graphics2D) arg0; 

    graphics.setColor(Color.WHITE); 
    graphics.fillRect(0, 0, (int) getWidth(), (int) getHeight()); 

任何幫助是非常感謝! -Craig

回答

2

嘗試從paintComponent(Graphics g)而不是paint(Graphics g)進行覆蓋。 paintComponent是您必須覆蓋成件繪圖的方法。

你確定你可以看到白色的矩形,嘗試使用紅色或其他你可以看到很好的東西。

+0

嗨 感謝您的回覆。雅,大綱肯定在那裏。我在紅色也嘗試過。我也嘗試把我的paint代碼放在paintComponent中,但是我得到了相同的結果。 我將繪畫JComponent直接添加到一個JDialog的中心,並且它完美地繪畫。但是,只要我將它添加到JScrollPane並將其放入JDialog中,我就開始獲得奇怪的繪圖行爲。 這裏是我添加到對話框/滾動條的代碼: – Craig 2010-02-02 18:48:42

+0

JScrollPane scrollPane = new JScrollPane(); scrollPane.add(containerCanvas); scrollPane.setSize(containerCanvas.getSize()); dialog.add(scrollPane,BorderLayout.CENTER); dialog.setVisible(true); – Craig 2010-02-02 18:49:18

1

好的,我想出了一個小小的答案。 與其說

scrollPane.add(containerCanvas); 

的我打電話

new JScrollPane(containerCanvas); 

此作品在某種意義上。但是,它現在不允許JScrollPane酒吧出現。我不知道這是爲什麼,但我目前正在研究它。但至少該組件正在再次繪製。

+0

爲了顯示滾動,只顯示PREFERRED大小,而不是getSize,這正是我在滾動組件中所做的。 如果無法使用組件實例化滾動窗格,則還可以調用scrollPane.getViewport.add(component)。這也是一樣的。 感謝大家看着這個。 – Craig 2010-02-02 19:41:16

2

看起來您正在取得進展,但您也可能會看到tutorial examples

Martijn Courteaux的分析是正確的:您應該覆蓋paintComponent()。另外,混合AWT和Swing組件也是一個壞主意。這兩個想法在Painting in AWT and Swing中討論。

滾動不應引起閃爍。以下是一個滾動組件網格並在背景上繪製棋盤的示例。

import java.awt.*; 
import javax.swing.*; 

public class Scrolling extends JFrame { 

    private static final int MAX = 8; 
    private static final int SIZE = 480; 
    private static final Color light = new Color(0x40C040); 
    private static final Color dark = new Color(0x408040); 

    private static class MyPanel extends JPanel { 

     public MyPanel() { 
      super(true); 
      this.setLayout(new GridLayout(MAX, MAX, MAX, MAX)); 
      this.setPreferredSize(new Dimension(SIZE, SIZE)); 
      for (int i = 0; i < MAX * MAX; i++) { 
       this.add(new JLabel(String.valueOf(i), JLabel.HORIZONTAL)); 
      } 
     } 

     @Override 
     public void paintComponent(final Graphics g) { 
      int w = this.getWidth()/MAX; 
      int h = this.getHeight()/MAX; 
      for (int row = 0; row < MAX; row++) { 
       for (int col = 0; col < MAX; col++) { 
        g.setColor((row + col) % 2 == 0 ? light : dark); 
        g.fillRect(col * w, row * h, w, h); 
       } 
      } 
     } 
    } 

    public Scrolling() { 

     this.setLayout(new BorderLayout()); 
     final MyPanel panel = new MyPanel(); 
     final JScrollPane scrollPane = new JScrollPane(panel); 
     scrollPane.getHorizontalScrollBar().setUnitIncrement(16); 
     scrollPane.getVerticalScrollBar().setUnitIncrement(16); 
     this.add(scrollPane, BorderLayout.CENTER); 
     this.pack(); 
     this.setSize(SIZE - SIZE/3, SIZE - SIZE/3); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setLocationRelativeTo(null); 
    } 

    public static void main(final String[] args) { 

     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new Scrolling().setVisible(true); 
      } 
     }); 
    } 
} 
+0

['setPreferredSize()'](http://stackoverflow.com/q/7229226/230513)僅用於演示目的。 – trashgod 2012-12-15 13:03:07