Java 如何将标签放置在另一个标签上?
是否可以将一个标签放在另一个标签上?我有一个具有特定背景颜色的标签,我想在上面放置另一个具有不同背景颜色的标签 可能吗?我希望smallBar位于bigBar之上。 我在各种活动中改变了smallBar的大小,所以我希望它能一直处于领先地位Java 如何将标签放置在另一个标签上?,java,swt,Java,Swt,是否可以将一个标签放在另一个标签上?我有一个具有特定背景颜色的标签,我想在上面放置另一个具有不同背景颜色的标签 可能吗?我希望smallBar位于bigBar之上。 我在各种活动中改变了smallBar的大小,所以我希望它能一直处于领先地位 public class GuessBarComposite extends Composite{ public GuessBarComposite(Composite shell, int style){ super(shell,style);
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
将标签放置在彼此的顶部,然后通过设置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();
}
}
你能提供你的代码吗?应该可以,谢谢!我使用了网格布局,还使用了top.moveover(底部)