简体   繁体   English

如何在SWT应用程序中更改主题

[英]How to change theme in SWT application

I'm new to developing SWT applications and am looking for an easy way to change the theme of a small application which I have already developed. 我是开发SWT应用程序的新手,并且正在寻找一种简便的方法来更改已经开发的小型应用程序的主题。

After doing some googling I'm able to tell that there seems to be something called presentations which seems like a way I can download a ready made theme and just apply it to my application. 进行了一些谷歌搜索之后,我可以断定似乎存在一种称为演示文稿的东西,这似乎是我可以下载一个现成的主题并将其应用于我的应用程序的一种方式。 Is that right? 那正确吗?

Alternatively can anyone point me to a good tutorial on the right way to go about it? 或者,有人可以指出正确的方法来指导我吗?

thanks 谢谢

If you are just talking about SWT without Ecipse RCP, then there is no way to theme the application. 如果您只是在谈论没有Ecipse RCP的SWT,那么就无法对应用程序进行主题化。

One of the main advantage of SWT is that it uses OS resources to resemble system applications. SWT的主要优点之一是它使用OS资源来类似于系统应用程序。 Using themes would contradict this approach. 使用主题会与这种方法相矛盾。

If you are however using Eclipse RCP 4, look at @Ran's answer. 但是,如果您使用的是Eclipse RCP 4,请查看@Ran的答案。

The org.eclipse.e4.ui.css.swt.theme extension point supports the creation of extensions for themes. org.eclipse.e4.ui.css.swt.theme扩展点支持创建主题扩展。 This extension defines an ID for the style and a pointer to the CSS file. 此扩展名定义样式的ID和指向CSS文件的指针。

You can also define the default theme via the cssTheme property in your org.eclipse.core.runtime.products extension. 您还可以通过org.eclipse.core.runtime.products扩展中的cssTheme属性定义默认主题。 This can also be used to define a fixed styling. 这也可以用于定义固定样式。

To switch the styling you use the IThemeEngine . 要切换样式,请使用IThemeEngine

package com.example.e4.rcp.todo.handlers;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;

public class ThemeSwitchHandler {
  // Remember the state
  static boolean defaulttheme= true;
  @Execute
  public void switchTheme(IThemeEngine engine) {
    System.out.println("ThemeSwitchHandler called");
    // The last argument controls
    // whether the change should be persisted and
    // restored on restart
    if (!defaulttheme) {
      engine.setTheme("com.vogella.e4.todo.defaulttheme", true);

    } else {
      engine.setTheme("com.vogella.e4.todo.redtheme", true);
    }
    defaulttheme= !defaulttheme;
  }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM