复合材料中的复合材料

时间:2011-07-27 16:42:13

标签: swt

是否有人知道如何或者是否可以在较大的复合材料中放置较小的复合材料。 例如,我希望较小的复合材料位于大型复合材料的中心并且可见,当在较大的复合材料中按下按钮时,较小的复合材料中会出现一张图片?

非常高兴你的帮助。 安。

1 个答案:

答案 0 :(得分:1)

我不确定我是否理解你的问题,你的意思是这样的......?

import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class CompositeInComposite {

    private Display display = null;
    private Shell shell = null;
    private Composite composite = null;
    private Image img = null;
    private URL dog = null;
    private URL cat = null;

    public CompositeInComposite() {
        display = new Display();
        shell = new Shell(display);
        shell.setLayout(new FillLayout(SWT.VERTICAL));
        shell.setSize(300, 300);

        Button btn = new Button(shell, SWT.PUSH);
        btn.setText("show cat");
        btn.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                try {
                    img = new Image(display, cat.openStream());
                    composite.redraw();
                } catch(Exception ex) {
                    ex.printStackTrace();
                }
            }

        });

        try {
            cat = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Collage_of_Six_Cats-02.jpg/250px-Collage_of_Six_Cats-02.jpg");
            dog = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/2/26/YellowLabradorLooking_new.jpg/260px-YellowLabradorLooking_new.jpg");
            img = new Image(display, dog.openStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

        composite = new Composite(shell, SWT.BORDER);
        composite.addPaintListener(new PaintListener() {

            @Override
            public void paintControl(PaintEvent e) {
                e.gc.drawImage(img, 0, 0);
            }
        });

//      shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

    public static void main(String[] args) {
        new CompositeInComposite();
    }
}

按钮的对齐方式,大小等等只是布局管理器的正确配置,我建议将MigLayout作为IMO最佳布局管理器。