如何消除 Windows 7 中丑陋的 SWT 按钮边缘?

发布于 2025-01-06 11:46:13 字数 1984 浏览 1 评论 0原文

我维护一个必须在 Windows XP 以及 Windows 7 中运行的 SWT/JFace 应用程序。 该应用程序有一个 TabFolder,其中包含一个 Composite,而后者又包含小部件。没有什么异常,一切正常,除了...


按钮

虽然它们在 Windows XP 中看起来不错,但在 Windows 7 中却有一个丑陋的灰色边框。

在此处输入图像描述 在此处输入图像描述


在此处输入图像描述

我可以手动设置按钮的背景,但我无法找出一个 找出父母实际背景颜色的方法。即使背景实际上是白色,getBackground() 也始终返回 240, 240, 240。

我发现在Windows XP中,TabFolder的背景是主题颜色3D对象,而在Windows 7中则没有这种对应关系。我试图设置每一个 主题颜色为亮红色,但 TabFolder 背景颜色没有留下深刻的印象。

因此,SWT 按钮本身没有正确设置其背景颜色,而且我看不出有什么方法可以通过编程找出正确的颜色。我应该怎么办?


无论如何,这里是重现该问题的完整最小示例的代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

public class TabTest {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);

        TabFolder tabFolder = new TabFolder (shell, SWT.NONE);
        TabItem item = new TabItem (tabFolder, SWT.NULL);
        item.setText ("Tab Item");

        Composite comp = new Composite(tabFolder, SWT.NONE);
        comp.setLayout(new RowLayout());
        item.setControl(comp);

        Button button = new Button(comp, SWT.PUSH);
        button.setText("Button ");
        tabFolder.setSize (160, 100);

        //Event Loop
        shell.pack ();
        shell.open ();
        while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
          display.sleep();
        }
        display.dispose();
    }
}

I maintain a SWT/JFace application that has to run in Windows XP as well as Windows 7.
The application has a TabFolder that contains a Composite, that in turn contains the widgets. Nothing unusual and everything works well except for...


The Buttons

While they look OK in Windows XP they have an ugly grey border in Windows 7.

enter image description here
enter image description here


enter image description here

I could set the background of the button manually but I failed to figure out a
way to find out the parents actual background color. getBackground() returns always 240, 240, 240 even if the background is actually white.

I found out that in Windows XP the background of a TabFolder is the theme color 3D Objects while in Windows 7 there is not such correspondence. I tried to set every single
theme color to bright red but the TabFolder background color was unimpressed.

So the SWT buttons don't set their background color correctly themselves and I see no way to find out the correct color programatically. What should I do?


Anyway, here is the code for a complete minimal example to reproduce the problem:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

public class TabTest {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);

        TabFolder tabFolder = new TabFolder (shell, SWT.NONE);
        TabItem item = new TabItem (tabFolder, SWT.NULL);
        item.setText ("Tab Item");

        Composite comp = new Composite(tabFolder, SWT.NONE);
        comp.setLayout(new RowLayout());
        item.setControl(comp);

        Button button = new Button(comp, SWT.PUSH);
        button.setText("Button ");
        tabFolder.setSize (160, 100);

        //Event Loop
        shell.pack ();
        shell.open ();
        while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
          display.sleep();
        }
        display.dispose();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2025-01-13 11:46:13

它看起来更像是一个平台问题而不是 SWT 问题。虽然不相关,但 存在一个平台特定错误 TabFolder 的 setBackground() ,它可能与您当前的问题相关,也可能无关。

一种解决方法是使用CTabFolder。请参阅以下代码片段和输出:

在 Windows 7 上输出,使用 JDK 1.6_b30、Eclipse 3.7

在此处输入图像描述

代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

public class TabTest 
{
    public static void main(String[] args) 
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout());
        shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        CTabFolder tabFolder = new CTabFolder (shell, SWT.BORDER|SWT.FLAT);
        tabFolder.setLayout(new GridLayout());
        tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        CTabItem item = new CTabItem(tabFolder, SWT.NULL);
        item.setText ("Tab Item");

        Composite comp = new Composite(tabFolder, SWT.NONE);
        comp.setLayout(new GridLayout());
        comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        item.setControl(comp);

        Button button = new Button(comp, SWT.PUSH);
        button.setText("Button ");
        tabFolder.setSize (160, 100);

        item = new CTabItem(tabFolder, SWT.NULL);
        item.setText ("Tab Item");

        //Event Loop
        shell.pack ();
        shell.open ();
        while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
          display.sleep();
        }
        display.dispose();
    }
}

It seems more like a platform issue than a SWT problem. Although not related but there was a platform specific bug for setBackground() of TabFolder, it may or may not be related to your current problem.

One workaround is to use CTabFolder. See the below snippet and output:

Output on Windows 7, using JDK 1.6_b30, Eclipse 3.7

enter image description here

Code:

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

public class TabTest 
{
    public static void main(String[] args) 
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout());
        shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        CTabFolder tabFolder = new CTabFolder (shell, SWT.BORDER|SWT.FLAT);
        tabFolder.setLayout(new GridLayout());
        tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        CTabItem item = new CTabItem(tabFolder, SWT.NULL);
        item.setText ("Tab Item");

        Composite comp = new Composite(tabFolder, SWT.NONE);
        comp.setLayout(new GridLayout());
        comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        item.setControl(comp);

        Button button = new Button(comp, SWT.PUSH);
        button.setText("Button ");
        tabFolder.setSize (160, 100);

        item = new CTabItem(tabFolder, SWT.NULL);
        item.setText ("Tab Item");

        //Event Loop
        shell.pack ();
        shell.open ();
        while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
          display.sleep();
        }
        display.dispose();
    }
}
殤城〤 2025-01-13 11:46:13

在包含按钮的组合中,将背景模式设置为 SWT.INHERIT_FORCE

    comp.setBackgroundMode(SWT.INHERIT_FORCE);

In your composite that holds the button set the background mode to SWT.INHERIT_FORCE

    comp.setBackgroundMode(SWT.INHERIT_FORCE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文