如果在SWT内部,为什么Swing组件会重新绘制三次?

时间:2014-03-22 13:39:52

标签: java swing graphics swt swt-awt

下面的代码显示了在SWT内部绘制的50个圆圈,其中Swing带有SWT_AWT桥。

每次调用paint都会导致调试打印。

很明显,涂料在运行中被调用三次。如果在普通Swing中运行,则只调用一次。

为什么在SWT内部被调用三次?

package tests;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Try_SWT_Swing {

    public static void main(String[] args) {

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        Composite composite = new Composite(shell, SWT.EMBEDDED | SWT.NO_BACKGROUND);
        composite.setLayout(new FillLayout());

        JPanel panel = new JPanel() {

            @Override
            protected void paintComponent(Graphics g) {

                System.out.println("paint");

                Graphics2D g2 = (Graphics2D) g;

                for(int i=1; i<50; ++i) {
                    g2.drawOval(i*10, i*10, 100, 100);
                }

            }
        };

        Frame frame = SWT_AWT.new_Frame(composite);
        frame.setLayout(new BorderLayout());
        frame.add(panel);

        shell.setSize(640, 480);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();

    }

}

0 个答案:

没有答案