Please Enable JavaScript!
Gon[ Enable JavaScript ]

이클립스(Eclipse) 로 SWT 설치후 프로젝트 개발 준비하기

자바(JAVA)
반응형

이클립스(Eclipse) SWT 설치후 프로젝트 개발 준비하기

 

개발환경 : JDK 1.6, Eclipse Indigo , window 7

 

먼저 이클립스를 다운받는다. Indigo 로 작업을 하였다.

기본적으로 다운 받았을 거라 믿고 SWT 라이브러리를 받으러 간다.

주소는 다음과 같다. http://www.eclipse.org/swt/

디자인은 예전하고 변한게 없으며 SWT 버전만 업그레이드 된것같다.

 

 그리고 예전에는 Development 부분에서 바로 받을수 있었는데 지금은

이글립스 다운로드 페이지로 가서 그 아래에 맞는 여러가지 플러그인

속에 포함되어있다. 이것은 이클립스 종속적인면을 감안해 헷갈리지 않도록

배려한 것 같다.  예전에는 SWT jar 만 받아서 이클립스 버전은 확인도

하지 않은체 설치 했다가 되니 안되니 실랑이 한적이 있었기에

시간도 절약되고 다른 플러그인이나 SDK 들도 모여있으니 여러모로

편리한 것 같다.

 

다운을 받았으면 이클립스를 실행시켜 프로젝트를 import 한다.

Jar 로 묶여 있는게 아니고 압축을 풀면 프로젝트 형태의 소스 파일이다.

Zip 파일을 풀지말고 import 시킬때 Existing Projects into Workspace

하게 되면 자동으로 프로젝트를 만들어주게 된다.

 

이렇게 만들어진 import 시켜 만들어진 SWT 프로젝트는 새로 만들어서

참조할 프로젝트에 Build path 시키면 링크가 된다. 그러면 SWT 라이브러리를

사용할수 있게 되는 것이다.

이제 프로젝트를 하나 만들어서 링크를 아래와 같이 시켜보자

 프로젝트의 오른 마우스 클릭후 Propertis 선택하자(Alt + Enter)

그리고 SWT 라이브러스 프로젝트를 선택한다

 다음은 라이브러리를 사용할 Main java class 를 하나 만든다.

단독 어플이므로 main 함수가 있어야 한다. 코딩을 직접하는거 보다 위자드에

체크만 하면 만들어지므로 아래 그림과 같이 선택한다

 이제 샘플코드로 프로그램을 실행해보자 이것은 체크 박스를 더블클릭하면 X 가 표시되고

해제되는 프로그램으로 체크박스에 색깔도 노란색으로 집어넣었다.

import org.eclipse.swt.SWT;
import org.eclipse.swt.accessibility.ACC;
import org.eclipse.swt.accessibility.AccessibleControlAdapter;
import org.eclipse.swt.accessibility.AccessibleControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

public class Main {

	/**
	 * @param args
	 */
	final static String[] ITEM_NAMES = {"first item", "second", "third", "fourth", "fifth"};

	public static void main (String [] args) {
		Display display = new Display ();
		final Image checkedImage = getStateImage (display, true);
		final Image uncheckedImage = getStateImage (display, false);

		Shell shell = new Shell (display);
		shell.setLayout (new FillLayout ());
		
		final Table table = new Table (shell, SWT.FULL_SELECTION | SWT.BORDER);
		for (int i = 0; i < ITEM_NAMES.length; i++) {
			TableItem item = new TableItem (table, SWT.NONE);
			item.setText (ITEM_NAMES[i]);
			item.setImage (i % 2 == 0 ? checkedImage : uncheckedImage);
		}
		table.addSelectionListener(new SelectionAdapter(){
			public void widgetDefaultSelected(SelectionEvent e) {
				TableItem item = (TableItem)e.item;
				item.setImage(item.getImage() == checkedImage ? uncheckedImage : checkedImage);
			}
		});

		table.getAccessible ().addAccessibleControlListener (new AccessibleControlAdapter () {
			public void getRole(AccessibleControlEvent e) {
				e.detail = ACC.ROLE_CHECKBUTTON;
			}
			public void getState (AccessibleControlEvent e) {
				if (e.childID >= 0 && e.childID < table.getItemCount ()) {
					TableItem item = table.getItem (e.childID);
					if (item.getImage() == checkedImage) {
						e.detail |= ACC.STATE_CHECKED;
					}
				}
			}
		});

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

	static Image getStateImage (Display display, boolean checked) {
		Image image = new Image (display, 16, 16);
		GC gc = new GC (image);
		gc.setBackground (display.getSystemColor (SWT.COLOR_YELLOW));
		gc.fillOval (0, 0, 16, 16);
		if (checked) {
			gc.setForeground (display.getSystemColor (SWT.COLOR_DARK_GREEN));
			gc.drawLine (0, 0, 16, 16);
			gc.drawLine (16, 0, 0, 16);
		}
		gc.dispose ();
		return image;
	}
}

 

이전에 awt 를 이용해 개발했을 때 보다 많이 편리했졌다. 위 환경설정하는

부분은 이것저것 손댈게 조금 있지만 앞으로 공부해 나가다 보면 알겠지만

SWT 는 이클립스에서 개발하기 편하도록 플러그인도 있으며 다른 Visual Studio 처럼

드래그앤 드랍으로 구현할수도 있으니 이 얼마나 편리한가

 

마지막으로 개발이 완료 되면 zip 파일 안에 있는 swt.jar 를 꺼내서 프로그램 실행시

사용하면 된다. 따로 프로젝트를 jar 로 묶어서 밷어낸후 사용할 필요가 없다.

반응형
Posted by 녹두장군1
,