[JAVA] SWT 기본 윈도우 예제

[JAVA] SWT 기본 윈도우 예제

자바에서 위 이미지처럼 윈도우를 띄우는 예제코드.

1. 자바 프로젝트에 SWT 라이브러리를 임포트한다. (ex : org.eclipse.swt.win32.win32.x86_64-4.3.jar)

2. 아래 코드를 붙여넣는다.

package com.bb.test;

import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SWTtest {
    public static boolean bInit = false;
    public static Text textBox1 = null;

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText(“Window Text”);
        shell.setLayout(new Layout() {

            @Override
            protected void layout(Composite arg0, boolean arg1) {
                // 윈도우 초기 사이즈 설정
                if (!bInit) {
                    arg0.setSize(800, 600);
                    bInit = true;
                }

                // 윈도우 크기에 따라 텍스트박스 사이즈 조절
                try {
                    if (textBox1 != null) {
                        textBox1.setBounds(10, 50, arg0.getSize().x – 40, arg0.getSize().y – 110);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected Point computeSize(Composite arg0, int arg1, int arg2, boolean arg3) {
                return null;
            }
        });

        // 버튼 생성
        Button button1 = new Button(shell, 0);
        button1.setBounds(10, 10, 150, 30);
        button1.setText(“Button Text”);

        // 텍스트박스 생성
        textBox1 = new Text(shell, 2626);
        textBox1.setLayoutData(new GridData(1808));
        textBox1.setFont(new Font(display, “굴림”, 20, 0));
        textBox1.setText(“Hello World”);
        textBox1.setFocus();

        // 레이블 생성
        Label label1 = new Label(shell, 0);
        label1.setBounds(200, 10, 150, 30);
        label1.setText(“Label Text”);

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