2

境界を設定しないと、以下のコードで期待どおりにラベルが表示されません。このコンポジット内に背景画像のみを表示したいので、シェル内にコンポジットを作成しています。

この場合、境界はどうあるべきですか?ラベルのテキストに基づいて最適な境界を取得できますか?

Display display = PlatformUI.createDisplay();
Shell shell = new Shell(display);
shell.setText("Header);

Composite main = new Composite(shell, SWT.NONE);
main.setBounds(10, 5, 775, 505);
InputStream is = getClass().getResourceAsStream("/resources/bg.png");
Image bg = new Image(display, is);
main.setBackgroundImage(bg);        
main.setBackgroundMode(SWT.INHERIT_DEFAULT);

Label label = new Label(main, SWT.NONE);            
//label.setBounds(0, 0, 400,100);           // not showing if commented away
label.setText("Label 1");
4

1 に答える 1

3

これは、Linux(Eclipse 3.6.2、Java 1.6.0.26)で機能します。

public class StackOverflow
{
    public static void main(String[] args)
    {
        Display display = Display.getDefault();
        Shell shell = new Shell(display);
        shell.setText("Header");

        Composite main = new Composite(shell, SWT.NONE);
        main.setLayout(new GridLayout(1, false));
        Image bg = new Image(display, "resources/bg.png");
        main.setBackgroundImage(bg);
        main.setBackgroundMode(SWT.INHERIT_DEFAULT);

        Label label = new Label(main, SWT.NONE);    
        label.setLayoutData(new GridData(GridData.FILL_BOTH));
        label.setText("Label 1");

        main.pack();
        main.setBounds(10, 5, 775, 505);
        shell.pack();
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }
}
于 2012-07-19T11:27:02.837 に答える