gpt4 book ai didi

Java SWT - 禁用按钮上的图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:38:27 27 4
gpt4 key购买 nike

我在我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com