2011-07-27 3 views
0

더 큰 합성물 안에 더 작은 합성물을 배치하는 방법이나 방법을 아는 사람이 있습니까? 예를 들어, 작은 합성물을 큰 합성물의 중앙에 보이게하고 큰 합성물에서 버튼을 누르면 작은 합성물에 그림이 나타납니다.합성물 내 합성물

귀하의 도움을 매우 기쁘게 생각합니다. Ann.

답변

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 최적의 레이아웃 매니저를 추천 할 것입니다.

관련 문제