繁体   English   中英

当我在SWT中更改按钮的文本时,整个按钮的大小都会更改。 如何禁用此功能?

[英]When I change the text for the button in SWT, the whole button size changes. How can I disable this?

我正在创建一个钢琴程序。 但是,当我将按钮文本更改为空时,按钮的大小也会改变。 如何禁用自动调整大小? 我是windowbuilder的新手。 我已经搜寻了一段时间,找不到与我的问题类似的东西。

package com.gmail.gogobebe2.piano;

import org.eclipse.swt.widgets.Composite;

public class Piano extends Composite {

    /**
     * Create the composite.
     * @param parent
     * @param style
     */
    public Piano(Composite parent, int style) {
        super(parent, style);
        setLayout(new GridLayout(9, false));
        new Label(this, SWT.NONE);
        new Label(this, SWT.NONE);
        new Label(this, SWT.NONE);
        new Label(this, SWT.NONE);
        new Label(this, SWT.NONE);
        new Label(this, SWT.NONE);
        new Label(this, SWT.NONE);
        new Label(this, SWT.NONE);
        new Label(this, SWT.NONE);
        new Label(this, SWT.NONE);

        Button btnNewButton = new Button(this, SWT.NONE);
        btnNewButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
            }
        });
        GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_btnNewButton.heightHint = 261;
        btnNewButton.setLayoutData(gd_btnNewButton);

        Button button_1 = new Button(this, SWT.NONE);
        button_1.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));

        Button button = new Button(this, SWT.NONE);
        button.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
        button.setText("New Button");

        Button button_2 = new Button(this, SWT.NONE);
        button_2.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
        button_2.setText("New Button");

        Button button_3 = new Button(this, SWT.NONE);
        button_3.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
        button_3.setText("New Button");

        Button button_4 = new Button(this, SWT.NONE);
        button_4.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
        button_4.setText("New Button");

        Button button_5 = new Button(this, SWT.NONE);
        button_5.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
        button_5.setText("New Button");

        Button button_6 = new Button(this, SWT.NONE);
        button_6.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
        button_6.setText("New Button");

    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }

}

这是更改按钮文字之前和更改按钮文字之后的一些屏幕截图

之前

之前

之后-将2个按钮的文字更改为空 后

要设置按钮的宽度,可以设置GridDatawidthHint属性。 在您的示例中,对于第一个按钮,您可以放置​​: gd_btnNewButton.widthHint = 100;

对于其他按钮,您可以更改类型的行:

button_1.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));

与:

GridData gd = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
gd.widthHint = 100;
button_1.setLayoutData(gd);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM