2010-02-02 31 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