Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在复合SWT Java中设置中心标签_Java_Swt_Label_Composite - Fatal编程技术网

如何在复合SWT Java中设置中心标签

如何在复合SWT Java中设置中心标签,java,swt,label,composite,Java,Swt,Label,Composite,我已经创建了一个JavaSWT应用程序,并且我有一个组合,其中包含一个维度不时变化的标签。标签显示图像并具有侦听器。 如何将标签居中放置在复合材料中 我创建了一个具有以下功能的shell: Composite conteinerBox = new Composite(shell, SWT.NONE); conteinerBox.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED)); conteinerBox.setBounds(342

我已经创建了一个JavaSWT应用程序,并且我有一个组合,其中包含一个维度不时变化的标签。标签显示图像并具有侦听器。 如何将标签居中放置在复合材料中

我创建了一个具有以下功能的shell:

Composite conteinerBox = new Composite(shell, SWT.NONE);
conteinerBox.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
conteinerBox.setBounds(342, 10, 410, 384);

Label imageContainer = new Label(conteinerBox,SWT.NONE);
imageContainer.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_DARK_SHADOW));
我使用了这个函数:

private void openImage() {

    if(isAnImage(file.getPath())){
        System.out.println("FILE OPEN : " + file.getPath());
        Image image = getImage(file.getPath());


        //attach image

          imageContainer.setSize(image.getBounds().width,image.getBounds().height);
        imageContainer.setImage(image);

                   //set image mouse listener
        imageContainer.addMouseMoveListener(new MouseMoveListener() {

            @Override
            public void mouseMove(MouseEvent event) {

                x_mouse_cordinate.setText("" + event.x);
                y_mouse_cordinate.setText("" + event.y);

            }
        });
}
包含图像的标签附着在点(0,0)上,但我想将其置于合成图的中心…建议?

请不要使用
setBounds
setSize
,除非必要,否则请使用
布局
s

请阅读:

下面是一些将
标签水平居中的示例代码:

public static void main(String[] args)
{
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, false));

    final Label centered = new Label(shell, SWT.NONE);
    centered.setText("Small text");
    centered.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true));

    Button changeText = new Button(shell, SWT.PUSH);
    changeText.setText("Change text");
    changeText.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            centered.setText("Loooooooooooooooooooong text");
            centered.getParent().layout();
        }
    });


    shell.pack();
    shell.setSize(600, 200);
    shell.open();
    while (!shell.isDisposed())
    {
        while (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
}
看起来像这样:


谢谢您的重播,但我对java SWT不是很有经验。@user3675796所以它解决了您的问题吗?如果是这样的话,请考虑接受我的回答。如果没有,请澄清。这个解决方案不适合我的情况,是我的帖子中更复杂的简单例子!感谢….@user3675796只需将
GridLayout
分配给
imageContainer
的父级,然后使用
SWT.CENTER
为前两个参数将
GridData
分配给
imageContainer
本身。更改图像时,请调用
imageContainer.getParent().layout()
。完成。