2017-08-04 18 views
0

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

+0

你有没有在你的shell中设置的布局,每个组合(包括壳牌)必须有一个布局。我使用了'shell.setLayout(new GridLayout());' –

+0

@ greg-449布局的问题是向我的shell添加布局会弄乱我的shell中的其他按钮和文本字段 - 有没有办法做到这一点,而无需向我的外壳添加布局? –

+0

你可以用setBounds搞定,但我无法帮助你,因为我从来没有用过。 –

回答

0

你可以只使用您与ProgressBar作为一个孩子画一个Composite

喜欢的东西:

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

enter image description here

+0

我试过这个,但没有奏效。查看代码和输出的问题。 –

相关问题