是否可以将一个标签放置在另一个标签之上?我有一个具有特定背景颜色的标签,我想在其上放置第二个具有不同背景颜色的标签。
这可能吗?我希望 SmallBar 位于 bigBar 之上。 我正在更改各种事件中的smallBar的大小,因此我希望它始终位于顶部。
public class GuessBarComposite extends Composite{
public GuessBarComposite(Composite shell, int style){
super(shell,style);
bigBar = new Label(this,SWT.NONE);
smallBar = new Label(this, SWT.NONE);
Color outOfRangeColor= new Color(Display.getDefault(), 139,0,0);
Color inRangeColor= new Color(Display.getDefault(), 255,140,0);
bigBar.setBounds(labelOffset,20,barWidth, barHeight);
bigBar.setBackground(outOfRangeColor);
smallBar.setBounds(labelOffset,20,barWidth-20, barHeight);
smallBar.setBackground(inRangeColor);
}
}
最佳答案
您可以使用 StackLayout
将 Label
放置在彼此的顶部,然后您可以通过设置 StackLayout#topControl
在它们之间切换>。这是一个例子:
public static void main(String args[])
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
final StackLayout stackLayout = new StackLayout();
final Composite stack = new Composite(shell, SWT.NONE);
stack.setLayout(stackLayout);
Label bottom = new Label(stack, SWT.NONE);
bottom.setText("Bottom");
Label top = new Label(stack, SWT.NONE);
top.setText("Top");
stackLayout.topControl = top;
Button button = new Button(shell, SWT.PUSH);
button.setText("Switch");
button.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
stackLayout.topControl = stackLayout.topControl.equals(top) ? bottom : top;
stack.layout(true, true);
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!shell.getDisplay().readAndDispatch())
shell.getDisplay().sleep();
}
}
<小时/>
如果您只是希望它们一个接一个地出现(就 y 位置而言位于顶部),则使用 GridLayout
:
public static void main(String args[])
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
Label bottom = new Label(shell, SWT.NONE);
bottom.setText("Bottom");
Label top = new Label(shell, SWT.NONE);
top.setText("Top");
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!shell.getDisplay().readAndDispatch())
shell.getDisplay().sleep();
}
}
关于java - 如何将一个标签放置在另一个标签之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24165096/