我正在创建一个钢琴程序。但是当我将按钮文本更改为空时,按钮的大小会发生变化。如何禁用自动调整大小?我对窗口构建器很陌生。我已经在谷歌上搜索了一段时间,但找不到任何与我的问题类似的内容。
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
}
}
这是更改按钮文本之前和更改按钮文本之后的一些屏幕截图
之前
最佳答案
要设置按钮的宽度,您可以设置 GridData
的 widthHint
属性。
在您的示例中,对于第一个按钮,您可以输入: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);
关于java - 当我更改 SWT 中按钮的文本时,整个按钮的大小会发生变化。我怎样才能禁用这个功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27725616/