Java SWT - 禁用按钮上的图像

标签 java image button swt

我在我的 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();
}

关于Java SWT - 禁用按钮上的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5395788/

相关文章:

java - 在 Eclipse 3.7 上安装 Android 开发工具的问题

java - 如何获得这个Java布局?

html - Bootstrap 3 : Responsive Image with fixed Hight should zoom in.

css - 将图像与表格单元格的边缘对齐

javascript - 使用 Javascript 制作按钮 html

java - 访问被拒绝发现属性“ro.sf.lcd_密度

java - Spring MVC模型对象相关的一些疑惑

python - 如何在 OpenCV(Python)中将灰度图像转换为 RGB?

android - 在按钮顶部添加水平分隔线

javascript - 当选择另一个单选按钮时,如何将单选按钮更改为 'checked'?