当我在SWT中更改按钮的文本时,整个按钮大小会发生变化。我怎么能禁用它?

时间:2014-12-31 19:35:45

标签: java eclipse user-interface swt windowbuilder

我正在创作一个钢琴课程。但是当我将按钮文本更改为空时,按钮的大小会发生变化。如何禁用自动调整大小?我对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
    }

}

以下是更改按钮文本之前和更改按钮文本之后的一些屏幕截图

之前

Before

之后 - 将2个按钮文本更改为无效 After

1 个答案:

答案 0 :(得分:2)

要设置按钮的宽度,您可以设置widthHint的{​​{1}}属性。 在您的示例中,您可以放置​​第一个按钮:GridData

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

gd_btnNewButton.widthHint = 100;

使用:

button_1.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
相关问题