如何将Label放在Button上?

时间:2022-09-27 22:55:20

The topic says it all, how can I place Label directly over Button? Tried multiple ways, nothing worked.. I can't use button.setTest("xxx"); in this case.

主题说明了这一点,我如何将Label直接放在Button上?试过多种方式,什么都行不通..我不能用button.setTest(“xxx”);在这种情况下。

EDIT: I thought that mentioning SWT library in tags would be enough. I'm kind of ashamed for the code, therefore I didn't want to post it.

编辑:我认为在标签中提到SWT库就足够了。我对代码感到羞愧,因此我不想发布它。

The thing is that I created a lot of buttons using for cycles, so I can't adress them by their variable name. My idea was creating another "layer" of labels and just have them over the buttons. The label should appear after the button is clicked.

问题是我创建了许多用于循环的按钮,因此我无法通过变量名来对它们进行加法。我的想法是创建另一个标签的“层”,并将它们放在按钮上。单击按钮后应出现标签。

MouseListener posluchac = new MouseAdapter() {
    @Override
    public void mouseDown(MouseEvent me) {
        for (int k = 0;k<sirka;k++)
        {
            for (int l = 0;l<vyska;l++)
            {
                if(me.getSource() == cudliky[k][l])
                {
                    System.out.println(k +" "+ l);
                    Label lbl4 = new Label(cmpst2, SWT.NONE);
                    lbl4.setText("X");
                    lbl4.setBounds((990-sirka*25)/2+k*25,(800-vyska*25)/2+l*25,25,25);
                }   
            }
        }
    }
}

This code creates labels, that are hidden.

此代码创建隐藏的标签。

2 个解决方案

#1


1  

There's no need to place a Label on top of the Button even if you don't want the button to have an initial text. This can all be achieved by using the right Layout or LayoutData. Here's an example using a GridLayout with 5 columns. The buttons initially don't have text on them, but do after you click on them (as you described in your question):

即使您不希望按钮具有初始文本,也无需在按钮顶部放置标签。这可以通过使用正确的Layout或LayoutData来实现。这是一个使用5列GridLayout的示例。按钮最初没有文本,但是在您点击它们之后(如您在问题中所述):

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(5, true));

    for (int i = 0; i < 30; i++)
    {
        Button button = new Button(shell, SWT.PUSH);
        button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        button.addListener(SWT.Selection, e -> button.setText(UUID.randomUUID().toString().substring(0, 10)));
    }

    shell.pack();
    shell.open();
    shell.setSize(600, 300);

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

Looks like this:

看起来像这样:

如何将Label放在Button上?

#2


0  

You have a typo.

你有一个错字。

button.setText("xxx"); // works

#1


1  

There's no need to place a Label on top of the Button even if you don't want the button to have an initial text. This can all be achieved by using the right Layout or LayoutData. Here's an example using a GridLayout with 5 columns. The buttons initially don't have text on them, but do after you click on them (as you described in your question):

即使您不希望按钮具有初始文本,也无需在按钮顶部放置标签。这可以通过使用正确的Layout或LayoutData来实现。这是一个使用5列GridLayout的示例。按钮最初没有文本,但是在您点击它们之后(如您在问题中所述):

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(5, true));

    for (int i = 0; i < 30; i++)
    {
        Button button = new Button(shell, SWT.PUSH);
        button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        button.addListener(SWT.Selection, e -> button.setText(UUID.randomUUID().toString().substring(0, 10)));
    }

    shell.pack();
    shell.open();
    shell.setSize(600, 300);

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

Looks like this:

看起来像这样:

如何将Label放在Button上?

#2


0  

You have a typo.

你有一个错字。

button.setText("xxx"); // works