SWTポジショニング– setBounds()またはsetLocation()
SWT GUIプログラミングの学習を開始するとき、テキストフィールド、ラベル、ボタン、その他のウィジェットをどのように配置するかを常に把握する必要があります。 SWTでは、setLocation()またはsetLocation()メソッドを使用して、ウィジェットまたはコンポーネントのサイズと位置を指定できます。
SWTがポジショニングに使用する2つの方法を次に示します。
1)setBounds(int x, int y, int witdh, int height) –ウィジェットのサイズと場所を設定します
2)setLocation(int x, int y) –ウィジェットの場所を設定します
位置は次のように左上隅から始まります
setBounds()の例
位置x = 100、y = 50、width = 300、height = 30でラベルを作成します
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class SWTPosition {
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell(display);
Label positiongLabel = new Label(shell, SWT.BORDER);
positiongLabel.setBounds(100,50,300,30);
positiongLabel.setText("My Position is : " + positiongLabel.getBounds());
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
setLocation()の例
位置x = 100、y = 50、width = 300、height = 30でラベルを作成します
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class SWTPosition {
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell(display);
Label positiongLabel = new Label(shell, SWT.BORDER);
positiongLabel.setSize(300,30);
positiongLabel.setLocation(100, 50);
positiongLabel.setText("My Position is : " + positiongLabel.getLocation());
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
setBounds()とsetLocation()の違いは何ですか?
上記の例のように、setBounds()メソッドとsetLocation()メソッドには大きな違いがないことに気付くかもしれません。 両方ともウィジェットの位置を指定できるため、SWTでウィジェットを複製するのはなぜですか? 私はそれについては知りませんが、setLocation()はウィジェットのサイズを指定するためにもう1つのsetSize()メソッドを指定する必要があります。 これは私が知っている唯一の違いです。