java - 寻求在 SWT 中显示图像的可靠方法

标签 java image swt

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

如果我的整个窗口仅由设置为 FillLayout 的复合 Material 组成,并且其唯一组件是 Canvas ,则图像将正确显示。

但是,一旦我尝试使用 GridLayoutRowLayout 以及更多组件在更复杂的窗口中显示图像,则图像不会显示在全部。

具体来说,我试图在用户单击按钮时更新图像。如果我在打开窗口时提供一张图像,那么该图像似乎显示得还不错,但如果我尝试在单击按钮时更新它,那么图像就会消失。

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

有人可以帮我解决这个问题吗?我花了一整天的时间来解决这个问题,但没有运气。

最佳答案

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

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();
}

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

enter image description here

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

enter image description here

关于java - 寻求在 SWT 中显示图像的可靠方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18875874/

相关文章:

java - Android 游戏时间处理

java - Java 中是否需要任何类型的 'enterprise container'?

JavaFX Image获取图像路径

image - wp7上的背景图像渲染问题

java - 即使在 SWT 中选择了 TreeItem,如何强制执行 TreeItem 的前景色

java - Birt 图表引擎文档

Java:方法签名中类型的子类

java - 开源 Java EE 通知框架

javascript - 如何使包含链接的 HTML 类成为链接?

java - 如何在启动时加载 eclipse 插件