【问题标题】:placing a widget in the background in SWT在 SWT 中将小部件放置在后台
【发布时间】:2018-01-12 13:29:41
【问题描述】:

我正在尝试显示进度条(使用 SWT 的 ProgressBar 类)。但是,由于我的窗口背景,该栏不是很明显。因此,我试图在进度条后面放置一个白色矩形(使用 GC.fillRoundedRectangle())。我找不到在矩形顶部显示进度条的方法。如何在 SWT 中实现“分层”?

谢谢!

编辑:我尝试了@greg-449 的建议,但我得到的只是一个空白窗口——我执行错了吗?

public static void main(String [] args){
    Display d = new Display();
    Shell parent = new Shell(d);
    parent.setSize(500, 500);
    parent.open();
    makeBar(parent);

    while (!parent.isDisposed()) {
        if (!d.readAndDispatch()) {
            d.sleep();
        }
    }
}

private static void makeBar(Shell parent) {
    Composite body = new Composite(parent, SWT.NONE);

    GridLayout layout = new GridLayout();
    layout.marginHeight = 20;
    layout.marginWidth = 20;
    body.setLayout(layout);

    body.addListener(SWT.Paint, event ->
      {
        Rectangle rect = body.getClientArea();
        event.gc.setBackground(body.getDisplay().getSystemColor(SWT.COLOR_WHITE));
        event.gc.fillRoundRectangle(rect.x, rect.y, rect.width, rect.height, 20, 20);
      });

    ProgressBar bar = new ProgressBar(body, SWT.HORIZONTAL);

    bar.setMaximum(100);
    bar.setSelection(40);       
}

这是我在运行这段代码时看到的:

Output of code

【问题讨论】:

  • 你还没有在你的 Shell 上设置布局,每个 Composite(包括 Shell)都必须有一个布局。我用shell.setLayout(new GridLayout());
  • @greg-449 布局的问题是向我的 shell 添加布局会弄乱我在 shell 上的其他按钮和文本字段 - 有没有办法在不添加布局的情况下做到这一点我的壳?
  • 你可以乱用 setBounds 但我不能帮你,因为我从不使用。

标签: java eclipse macos swt


【解决方案1】:

您可以只使用Composite,小时候用ProgressBar 绘画。

类似:

Composite body = new Composite(parent, SWT.NONE);

GridLayout layout = new GridLayout();
layout.marginHeight = 20;
layout.marginWidth = 20;
body.setLayout(layout);

body.addListener(SWT.Paint, event ->
  {
    Rectangle rect = body.getClientArea();
    event.gc.setBackground(body.getDisplay().getSystemColor(SWT.COLOR_WHITE));
    event.gc.fillRoundRectangle(rect.x, rect.y, rect.width, rect.height, 20, 20);
  });

ProgressBar bar = new ProgressBar(body, SWT.HORIZONTAL);

bar.setMaximum(100);
bar.setSelection(40);

【讨论】:

  • 我试过这个,但是没有用。有关代码和输出,请参阅问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多