Eclipseで初期ウインドウを設定するにはどうすればよいですか。
Eclipseで初期ウィンドウの設定方法は、Windowクラスの生成と設定で行います。
- Javaクラスを新規に作成し、Windowという名前を付けます。
- ウィンドウクラスではorg.eclipse.jface.window.ApplicationWindowクラスを継承します。
- Window クラスで、ウィンドウのコンテンツを作成する createContents(Composite parent) メソッドをオーバーライドする
- CompositeとLabel、Text、Buttonなどのウィジェットを使って、createContentsメソッドの中でウィンドウのレイアウトを作成します。
- Windowクラスで、ウィンドウのスタイルやプロパティを設定するconfigureShell(Shell shell)メソッドをオーバーライドします。
- configureShellメソッドで、shellオブジェクトのメソッド(setText、setMinimumSize、setLocationなど)を使って、タイトル、最小サイズ、位置などのウィンドウの設定を行います。
- Mainという名前の新しいJavaクラスを作成し、そのクラスにmainメソッドを追加する。
- 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」クラスを実行することでウィンドウが起動されます。このサンプルコードでは、ラベルとボタンを含んだウィンドウを作成し、ウィンドウのタイトル、最小サイズ、位置を設定しています。