-4

ツールアイテムに画像を設定したい。しかし、私のプログラムは機能せず、空のウィンドウが表示されるだけです。

これまでの私のコード:

import org.eclipse.swt.*;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;

public class test
{
    public static void main(String [] args)
    {
        Display display = new Display( );
        final Shell shell = new Shell(display);
        shell.setSize(900, 200);

        final ToolBar t=new ToolBar(shell,SWT.NONE);
        Image image=new Image(display,"C:\\Users\\ART\\Documents\\Parsian\\bottum.jpg");

        final ToolItem ti=new ToolItem(t,SWT.NONE);
        ti.setImage(image);
        ti.setText("test");

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

1 に答える 1

1

あなたのコードに欠けているのは魔法の関数Control#pack()です:

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);

    final ToolBar bar = new ToolBar(shell, SWT.FLAT | SWT.WRAP | SWT.RIGHT);
    final ToolItem item = new ToolItem(bar, SWT.PUSH);
    Image image = new Image(display, "img/star.png");
    item.setImage(image);
    item.setText("test");

    /* Add this pack() call */
    bar.pack();
    shell.setSize(400, 250);
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

次のようになります。

ここに画像の説明を入力してください

于 2012-12-11T22:03:03.397 に答える