我在我的SWT Gui中使用带有图像的按钮,有时需要禁用它们。
这会让SWT从我之前放置在按钮上的彩色图像创建一个自动禁用的图像,而禁用的图标看起来并不好看。我想在按钮上放置自己的禁用图像,但我找不到方法。
有吗?
你无法对Button进行子类化,因此你需要在启用/禁用按钮时进行操作。我无法找到SWT -> Windows事件映射ID来调用addListener
以使其更通用。
public static void main(String[] args) throws Exception
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, true));
final Image image1 = new Image(null, new FileInputStream(
"C:\\tmp\\enabled.png"));
final Image image2 = new Image(null, new FileInputStream(
"C:\\tmp\\disabled.png"));
final Button button= new Button(shell, SWT.NONE);
button.setText("Hello World SWT");
button.setImage(image1);
button.pack();
final Button cb = new Button(shell, SWT.CHECK);
cb.setText("Check me");
cb.addSelectionListener(new SelectionAdapter()
{
@Override
public void widgetSelected(SelectionEvent event)
{
button.setEnabled(!cb.getSelection());
if (button.isEnabled())
button.setImage(image1);
else
button.setImage(image2);
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
我通过修改Button类本身来解决了我的问题。现在它会缓存禁用的图像,并在需要时重复使用它们。