【问题标题】:When I change the text for the button in SWT, the whole button size changes. How can I disable this?当我在 SWT 中更改按钮的文本时,整个按钮的大小都会发生变化。我怎样才能禁用它?
【发布时间】:2016-11-26 00:37:06
【问题描述】:

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

【问题讨论】:

    标签: java eclipse user-interface swt windowbuilder


    【解决方案1】:

    要设置按钮的宽度,您可以设置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);
    

    【讨论】:

    • 谢谢!我已经完成了我的钢琴! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    相关资源
    最近更新 更多