1

我只是在 SWT 中显示小(40x40)像素图像时遇到了很多麻烦。

如果我的整个窗口只包含一个设置为 的合成FillLayout,并且它的唯一组件是一个画布,那么图像会正确显示。

但是,一旦我尝试在具有一个GridLayoutRowLayout多个组件的更复杂的窗口中显示图像,则该图像根本不会显示。

具体来说,我试图在用户单击按钮时更新图像。如果我在打开窗口时提供了一张图片,那张图片看起来还不错,但是如果我在单击按钮时尝试更新它,那么图片就会消失。

因此,简单来说,我正在寻找一种显示图像的方法,该方法允许根据用户输入(按钮单击等)更新图像,并且在必要时能够扩展布局以显示图像。

谁能帮我这个?我已经花了一整天的时间没有运气。

4

1 回答 1

2

不确定您的问题到底是什么,但这段代码对我有用:

private static Display  display = Display.getDefault();
private static Composite[]  cells  = new Composite[6];
private static Color[] colors = new Color[6];

private static Random   random  = new Random(System.currentTimeMillis());

private static PaintListener listener  = new PaintListener()
{
    @Override
    public void paintControl(PaintEvent e)
    {
        Rectangle rect = ((Canvas) e.widget).getBounds();
        e.gc.setBackground(colors[random.nextInt(colors.length)]);
        e.gc.fillRectangle(5, 5, rect.width - 10, rect.height - 10);
    }
};

static
{
    int i = 0;
    colors[i++] = display.getSystemColor(SWT.COLOR_BLACK);
    colors[i++] = display.getSystemColor(SWT.COLOR_RED);
    colors[i++] = display.getSystemColor(SWT.COLOR_BLUE);
    colors[i++] = display.getSystemColor(SWT.COLOR_GREEN);
    colors[i++] = display.getSystemColor(SWT.COLOR_YELLOW);
    colors[i++] = display.getSystemColor(SWT.COLOR_GRAY);
}

public static void main(String[] args)
{
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(3, true));

    for (int i = 0; i < 6; i++)
    {
        Composite comp = new Composite(shell, SWT.NONE);
        comp.setLayout(new GridLayout(2, false));
        Canvas canvas = new Canvas(comp, SWT.NONE);
        canvas.addPaintListener(listener);
        Label text = new Label(comp, SWT.NONE);
        String textString = "";
        for(int j = 0; j < random.nextInt(10); j++)
            textString += "A";

        text.setText(textString);

        cells[i] = comp;
    }

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Random");
    button.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            for (int i = 0; i < 6; i++)
            {
                Composite cell = cells[i];
                for(Control child : cell.getChildren())
                    child.dispose();

                Canvas canvas = new Canvas(cell, SWT.NONE);
                canvas.addPaintListener(listener);

                Label text = new Label(cell, SWT.NONE);
                String textString = "";
                for(int j = 0; j < random.nextInt(10); j++)
                    textString += "A";

                text.setText(textString);
            }

            shell.layout(true,  true);
            shell.pack();
        }
    });

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

在启动时创建类似这样的内容:

在此处输入图像描述

并在按下按钮时随机更改图像:

在此处输入图像描述

于 2013-09-18T15:13:22.207 回答