【问题标题】:SWT Button - Remove button backgroundSWT 按钮 - 删除按钮背景
【发布时间】:2014-11-24 16:22:02
【问题描述】:

我有一个如下所示的按钮:

我想删除按钮背景。也就是说,图像将是按钮。 这是我的代码:

Button button = new Button(shell, SWT.NONE);
button.setToolTipText("Email Settings...");
button.setImage(SWTResourceManager.getImage(Settings.class, "settings1616.png"));
button.setBounds(494, 26, 28, 25);

我试过了..

button.setBackground(null);
button.setBackgroundImage(null);

但它不会删除任何东西。 谢谢!

【问题讨论】:

  • 这是什么平台?在某些平台(例如 Windows)上,您不能像这样更改按钮背景 - stackoverflow.com/q/3312102/2670892
  • 我使用的是 Windows。这可以通过 Macintosh 实现吗?
  • 或者只是使用带有 ImageIcon 的可点击 JLabel
  • 好的,我试试看。
  • 快速测试表明 setBackground 在 Mac 中不起作用。

标签: java eclipse eclipse-plugin


【解决方案1】:

请尝试使用 SWT Image HyperLink ,这没有按钮背景,它解决了你的目的。

org.eclipse.ui.forms 添加为依赖项。

ImageHyperlink hyperlink = new ImageHyperlink(parentComposite, SWT.NONE); hyperlink.setImage("image location");

【讨论】:

  • 太棒了.. 非常感谢 :)
【解决方案2】:

将工具项与工具栏一起使用。这里http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet18.java 是一个可以提供帮助的sn-p。 并使用

toolitem.setImage(Your_image);

这也适用于 mac!

【讨论】:

    【解决方案3】:

    跳出框框思考。

    Label button = new Label(parent, SWT.BORDER);
    button.setToolTipText("Email Settings...");
    button.setImage(SWTResourceManager.getImage(Settings.class, "settings1616.png"));
    button.setBounds(494, 26, 28, 25);
    button.setCursor(Display.getDefault().getCursor(SWT.CURSOR_HAND)); 
    button.addMouseListener(new MouseAdapter()
    {
        @Override public void mouseClicked(MouseEvent e)
        {
            // Do your thing
        }
    });
    

    【讨论】: