开发者

composite within a composite

开发者 https://www.devze.com 2023-03-23 13:36 出处:网络
Does anybody know how or if you can place a smaller composite inside a larger composite. For example I want the smaller composite to be in the centre of the large composite and visible and when a butt

Does anybody know how or if you can place a smaller composite inside a larger composite. For example I want the smaller composite to be in the centre of the large composite and visible and when a button is pressed in the larger composite a picture appears in the smaller composite?

Would be extremely glad of your help开发者_如何学C. Ann.


I'm not sure if I understand your question, you meant something like this..?

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();
    }
}

The alignment of the button, it's size, etc. is just a proper configuration of layout manager, I would recommend MigLayout as IMO best layout manager that exists.

0

精彩评论

暂无评论...
验证码 换一张
取 消