Eclipseで初期ウインドウを設定するにはどうすればよいですか。

Eclipseで初期ウィンドウの設定方法は、Windowクラスの生成と設定で行います。

  1. Javaクラスを新規に作成し、Windowという名前を付けます。
  2. ウィンドウクラスではorg.eclipse.jface.window.ApplicationWindowクラスを継承します。
  3. Window クラスで、ウィンドウのコンテンツを作成する createContents(Composite parent) メソッドをオーバーライドする
  4. CompositeとLabel、Text、Buttonなどのウィジェットを使って、createContentsメソッドの中でウィンドウのレイアウトを作成します。
  5. Windowクラスで、ウィンドウのスタイルやプロパティを設定するconfigureShell(Shell shell)メソッドをオーバーライドします。
  6. configureShellメソッドで、shellオブジェクトのメソッド(setText、setMinimumSize、setLocationなど)を使って、タイトル、最小サイズ、位置などのウィンドウの設定を行います。
  7. Mainという名前の新しいJavaクラスを作成し、そのクラスにmainメソッドを追加する。
  8. mainメソッドで、Windowクラスのインスタンスを作成してrun()メソッドを呼び出し、ウィンドウを開始する。

以下にサンプルコードを示します。

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class Window extends ApplicationWindow {

    public Window() {
        super(null);
    }

    @Override
    protected Control createContents(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        container.setLayout(new FillLayout());

        Label label = new Label(container, SWT.NONE);
        label.setText("Hello, Eclipse!");

        Button button = new Button(container, SWT.PUSH);
        button.setText("Click Me!");

        return container;
    }

    @Override
    protected void configureShell(Shell shell) {
        super.configureShell(shell);
        shell.setText("Eclipse Window");
        shell.setMinimumSize(400, 300);
        shell.setLocation(200, 200);
    }

    public static void main(String[] args) {
        Window window = new Window();
        window.setBlockOnOpen(true);
        window.open();

        Display.getCurrent().dispose();
    }
}

「Main」クラスを実行することでウィンドウが起動されます。このサンプルコードでは、ラベルとボタンを含んだウィンドウを作成し、ウィンドウのタイトル、最小サイズ、位置を設定しています。

bannerAds