SWTのGUIプログラミングについて学ぶとき、我々はいつもテキストフィールド、ラベル、ボタン、および他のウィジェットをどのように配置するかを理解したい。 SWTでは、

setLocation()

または

setLocation()

メソッドを使用して、ウィジェットまたはコンポーネントのサイズと位置を指定できます。

SWTが測位に使用する2つの方法は次のとおりです。

setLocation(int x、int y)** – ウィジェットの位置を設定します。setLocation(int x、int y、int width、int height)

位置は、次の画像のように左上隅から始まります://wp-content/uploads/2008/12/swt-position-1.jpg[image、title = “swt-position-1”]

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()はウィジェットのサイズを指定するためにsetSize()メソッドをもう1つ指定する必要があります。これは私が知っている唯一の違いです。